채택완료

접속자 visit_hour.php 합산이 아닌 일별 출력

2018-06-10 (일) 21:38:47 2,565

아래 코드는 그누보드5 ./adm/visit_hour.php 파일 입니다.

Copy
<?php
$sub_menu = "200800";
include_once('./_common.php');

auth_check($auth[$sub_menu], 'r');

$g5['title'] = '시간별 접속자집계';
include_once('./visit.sub.php');

$colspan = 4;

$max = 0;
$sum_count = 0;

 

$fr_date = "2018-05-08"; // 시작일

$to_date = "2018-05-09"; // 종료일

 

$sql = " select SUBSTRING(vi_time,1,2) as vi_hour, count(vi_id) as cnt
            from {$g5['visit_table']}
            where vi_date between '{$fr_date}' and '{$to_date}'
            group by vi_hour
            order by vi_hour ";
$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++) {
    $arr[$row['vi_hour']] = $row['cnt'];

    if ($row['cnt'] > $max) $max = $row['cnt'];

    $sum_count += $row['cnt'];
}

print_r($arr);   // 출력값 확인하기 위해 임시 추가
?>
Copy
<?php
    $k = 0;
    if ($i) {
        for ($i=0; $i<24; $i++) {
            $hour = sprintf("%02d", $i);
            $count = (int)$arr[$hour];

            $rate = ($count / $sum_count * 100);
            $s_rate = number_format($rate, 1);

            $bg = 'bg'.($i%2);
    ?>
    <tr class="<?php echo $bg; ?>">
        <td class="td_category"><?php echo $hour ?></td>
        <td>
            <div class="visit_bar">
                <span style="width:<?php echo $s_rate ?>%"></span>
            </div>
        </td>
        <td class="td_numbig"><?php echo number_format($count) ?></td>
        <td class="td_num"><?php echo $s_rate ?></td>
    </tr>
    <?php
        }
    } else {
        echo '<tr><td colspan="'.$colspan.'" class="empty_table">자료가 없습니다.</td></tr>';
    }
    ?>

현재 시간별 접속자 뽑으려고 하는데 2일치 누적되어 출력이 되고 있습니다.

결과 :

Array ( [] => Array ( [00] => 17 [01] => 7 [02] => 2 [03] => 15 [04] => 4 [05] => 3 [06] => 5 [07] => 2 [08] => 7 [09] => 5 [10] => 4 [11] => 3 [12] => 5 [13] => 5 [14] => 1 [15] => 5 [16] => 2 [17] => 3 [18] => 2 [19] => 7 [20] => 3 [21] => 2 [22] => 1 [23] => 4 ) ) // 누적 24시간 누적 자료

이걸 시간별 출력

Array ( [] => Array ( [00] => 1 [01] => 3 [02] => 2 [03] => 4 [04] => 3 [05] => 2 [06] => 2 [08] => 3 [10] => 1 [12] => 2 [13] => 4 [16] => .......(생략)....... [45] => 2 [46] => 3 [47] => 1 ) )

이렇게 바꾸려서 쿼리 뭐라 입력 해야 되는지 궁금 합니다.

앞서 질문을 올려 답변을 받았으나 결과가 나오지 않아 다시 질문을 올립니다. 죄송합니다.

eyekiss 님의 답변 : https://i.imgur.com/rSbevRS.jpg

답변 1개 / 댓글 3개

채택된 답변
+20 포인트

이 정도면 제작의뢰 수준이네요..^^;;

그냥 제가 전체소스를 수정했습니다.

오류나면 말씀하세요.

Copy
<?php

$sub_menu = "200800";

include_once('./_common.php');

 

auth_check($auth[$sub_menu], 'r');

 

$g5['title'] = '시간별 접속자집계';

include_once('./visit.sub.php');

 

$colspan = 4;

 

$max = 0;

$sum_count = 0;

$arr = $date_count = array();

$sql = " select SUBSTRING(vi_time,1,2) as vi_hour, count(vi_id) as cnt, vi_date

from {$g5['visit_table']}

where vi_date between '{$fr_date}' and '{$to_date}'

group by vi_hour

order by vi_hour ";

$result = sql_query($sql);

for ($i=0; $row=sql_fetch_array($result); $i++) {

//$arr[$row['vi_hour']] = $row['cnt'];

    $arr[$row['vi_date']][$row['vi_hour']] = $row['cnt'];

 

if ($row['cnt'] > $max) $max = $row['cnt'];

 

//$sum_count += $row['cnt'];

    $date_count[$row['vi_date']] += $row['cnt']; //일자별 합계

$sum_count += $row['cnt']; //전체 합계

}

?>

 

<div class="tbl_head01 tbl_wrap">

<table>

<caption><?php echo $g5['title']; ?> 목록</caption>

<thead>

<tr>

<th scope="col">시간</th>

<th scope="col">그래프</th>

<th scope="col">접속자수</th>

<th scope="col">비율(%)</th>

</tr>

</thead>

<tfoot>

<tr>

<td colspan="2">합계</td>

<td><strong><?php echo number_format($sum_count) ?></strong></td>

<td>100%</td>

</tr>

</tfoot>

<tbody>

<?php

$k = 0;

if ($i) {

for ($d=strtotime($fr_date); $d<=strtotime($to_date); $d+=60*60*24) { // 오류 인것 같아요.

$date = date('Y-m-d',$d);

for ($i=0; $i<24; $i++) {

$hour = sprintf("%02d", $i);

//$count = (int)$arr[$hour];

$count = (int)$arr[$date][$hour];

 

//$rate = ($count / $sum_count * 100);

if($date_count[$date]) $rate = ($count / $date_count[$date] * 100);

else $rate=0;

$s_rate = number_format($rate, 1);

 

$bg = 'bg'.($i%2);

?>

<tr class="<?php echo $bg; ?>">

<td class="td_category"><?php echo $hour ?></td>

<td>

<div class="visit_bar">

<span style="width:<?php echo $s_rate ?>%"></span>

</div>

</td>

<td class="td_numbig"><?php echo number_format($count) ?></td>

<td class="td_num"><?php echo $s_rate ?></td>

</tr>

<?php

}

?>

<tr>

<td colspan="2"><?=$date?> 합계</td>

<td><strong><?php echo number_format($date_count[$date]) ?></strong></td>

<td>100%</td>

</tr>

<?

}

} else {

echo '<tr><td colspan="'.$colspan.'" class="empty_table">자료가 없습니다.</td></tr>';

}

?>

</tbody>

</table>

</div>

 

<?php

include_once('./admin.tail.php');

?>

답변에 대한 댓글 3개

죄송 합니다..
오류가 있는것 같아요.,

05월 01일 : https://i.imgur.com/CNYYfkU.jpg 00시 4명
05월 02일 : https://i.imgur.com/DX8ii5t.jpg 00시 11명

이틀치 : https://i.imgur.com/yqOw3me.jpg 5월 1일 00시 15명 / 5월 2일 00시 0명

자세히 보면 5월 1일 방문하지 않은 시간대 5월 2일 표시되는것 같아요.
그리고 이틀치 스크린샷 결과에서 5월 1일 00시 시간대 부터 이틀치 더한 값이 나오는 같구요.
제가 실수가 있었네요..
18~19줄 수정해보세요.
[code]
group by vi_date, vi_hour
order by vi_date, vi_hour ";
[/code]
늦은 시간까지 안되는 부분 체크 해주셔서 정말 감사드립니다.
복 받으실꺼에요..

답변을 작성하려면 로그인이 필요합니다.