시간별 접속자 관련 문의
본문
관리자 메인페이지에 시간별 접속자 차트(그래프)작업을 하고 있는 초보자 입니다.
시간항목, 오늘 접속자 출력까지는 시간별 출력이 잘되고 있습니다.
문제는 어제 접속자와 2일전(엊그제) 접속자 출력이 시간별로 출력이 되는게 아니라 총 접속자 수가 표시가 됩니다.
이리저리 찾아보고 테스트를 해봐도 시간별로 접속자수가 표시가 되지 않네요..ㅠㅠ
너무 초보인지라 고수님들의 도움을 받고자 이렇게 문의 드립니다.
사용된 소스코드는 아래와 같습니다.
<?php
//sql 쿼리
$sql_common =" select SUBSTRING(vi_time,1,2) as vi_hour, count(vi_id) as cnt from {$g5['visit_table']} where vi_date between ";
// 어제
$sql = "$sql_common '" . date("Y-m-d",strtotime("-1 day")) . "' and '" . date("Y-m-d",strtotime("-1 day")) . " 23:59:59'";
$result = sql_fetch($sql);
// 엊그제(2일전)
$sql = "$sql_common '" . date("Y-m-d",strtotime("-2 day")) . "' and '" . date("Y-m-d",strtotime("-2 day")) . " 23:59:59'";
$result = sql_fetch($sql);
?>
<!-- #adm_visitor_time -->
<div id="sh_adm_visitor_time">
<?php
$max = 0;
$sum_count = 0;
$arr = array();
$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'];
}
?>
시간별 접속자 차트
<h3>시간별 방문자 현황 (최근 3일)<a href=""></a></h3>
<div class="graph_wrap">
<div id="visitor_graph"></div>
<script>
var eg2 = new Easygraphs({
container: document.getElementById('visitor_graph'),
width: 550,
height: 160,
padding: {
top: 30,
right: 30,
left: 65
},
tooltip: {
template: '{{ category }}: {{ value }}',
widthAuto: true,
color: '#222',
background: '#FFF'
},
yAxis: {
text: {
toFixed: 0
},
title: {
text: '방문자수'
},
grid: {
show: false
}
},
xAxis: {
labels: [
<?php
$k = 0;
if ($i) {
for ($i=0; $i<24; $i++) {
$hour = sprintf("%02d", $i);
$count = isset($arr[$hour]) ? (int) $arr[$hour] : 0;
$rate = ($count / $sum_count * 100);
$s_rate = number_format($rate, 1);
?> <?php echo $hour ?>
<?php
}
}
?>
]
},
data: [
{
name: '오늘',
dots: {
color: 'rgb(3, 169, 245)'
},
line: {
width: 1,
color: 'rgb(3, 169, 245)'
},
values: [
<?php
$k = 0;
if ($i) {
for ($i=0; $i<24; $i++) {
$hour = sprintf("%02d", $i);
$count = isset($arr[$hour]) ? (int) $arr[$hour] : 0;
$rate = ($count / $sum_count * 100);
$s_rate = number_format($rate, 1);
?> <?php echo number_format($count) ?>
<?php
}
}
?>
]
},
{
name: '어제',
dots: {
color: 'rgb(255, 140, 4)'
},
line: {
width: 1,
color: 'rgb(255, 140, 4)'
},
values: [
<?php
$k = 0;
if ($i) {
for ($i=0; $i<24; $i++) {
$hour = sprintf("%02d", $i);
$count = isset($arr[$hour]) ? (int) $arr[$hour] : 0;
$rate = ($count / $sum_count * 100);
$s_rate = number_format($rate, 1);
?> <?php echo number_format($result['cnt']) ?>
<?php
}
}
?>
]
},
{
name: '엊그제',
dots: {
color: 'rgb(97, 108, 119)'
},
line: {
width: 1,
color: 'rgb(97, 108, 119)'
},
values: [
<?php
$k = 0;
if ($i) {
for ($i=0; $i<24; $i++) {
$hour = sprintf("%02d", $i);
$count = isset($arr[$hour]) ? (int) $arr[$hour] : 0;
$rate = ($count / $sum_count * 100);
$s_rate = number_format($rate, 1);
?> <?php echo number_format($result['cnt']) ?>
<?php
}
}
?>
]
}
]
});
eg2.render();
</script>
</div>
</div>
<!-- #adm_visitor_time -->
시간
labels: [ '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', ]
오늘 방문자
values: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, ] //시간별 접속자 정상출력
어제 방문자
values: [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, ] // 시간별 접속자 총합계 출력
엊그제 방문자
values: [ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, ] // 시간별 접속자 총합계 출력
시간과 오늘 방문자는 정상적으로 출력이 되는데 어제, 엊그제 방문자수가 총 합계로 출력이 되네요..
고수님들의 조언 및 도움 부탁드리겠습니다.
오늘도 즐거운 하루 되세요~
!-->!-->
답변 2
우선 아래 소스는 총합계라 필요 없는 부분이구요..
<?php
//sql 쿼리
$sql_common =" select SUBSTRING(vi_time,1,2) as vi_hour, count(vi_id) as cnt from {$g5['visit_table']} where vi_date between ";
// 어제
$sql = "$sql_common '" . date("Y-m-d",strtotime("-1 day")) . "' and '" . date("Y-m-d",strtotime("-1 day")) . " 23:59:59'";
$result = sql_fetch($sql);
// 엊그제(2일전)
$sql = "$sql_common '" . date("Y-m-d",strtotime("-2 day")) . "' and '" . date("Y-m-d",strtotime("-2 day")) . " 23:59:59'";
$result = sql_fetch($sql);
?>
3일간 데이터를 불러와서 날짜별로 저장을 합니다.
$fr_date = date("Y-m-d",strtotime("-2 day"));
$to_date = date("Y-m-d");
$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_date']][$row['vi_hour']] = $row['cnt'];
if ($row['cnt'] > $max) $max = $row['cnt'];
$sum_count += $row['cnt'];
}
출력 부분을 날짜별로 출력을 합니다.
<?php
$k = 0;
if ($i) {
$date = date("Y-m-d");
for ($i=0; $i<24; $i++) {
$hour = sprintf("%02d", $i);
$count = isset($arr[$date][$hour]) ? (int) $arr[$hour] : 0;
$rate = ($count / $sum_count * 100);
$s_rate = number_format($rate, 1);
?> <?php echo number_format($count) ?>
<?php
}
}
?>
<?php
$k = 0;
if ($i) {
$date = date("Y-m-d",strtotime("-1 day"));
for ($i=0; $i<24; $i++) {
$hour = sprintf("%02d", $i);
$count = isset($arr[$date][$hour]) ? (int) $arr[$hour] : 0;
$rate = ($count / $sum_count * 100);
$s_rate = number_format($rate, 1);
?> <?php echo number_format($count) ?>
<?php
}
}
?>
<?php
$k = 0;
if ($i) {
$date = date("Y-m-d",strtotime("-2 day"));
for ($i=0; $i<24; $i++) {
$hour = sprintf("%02d", $i);
$count = isset($arr[$date][$hour]) ? (int) $arr[$hour] : 0;
$rate = ($count / $sum_count * 100);
$s_rate = number_format($rate, 1);
?> <?php echo number_format($count) ?>
<?php
}
}
?>
" select SUBSTRING(vi_time,1,2) as vi_hour, count(vi_id) as cnt from {$g5['visit_table']} where vi_date between ";
where 끝나고
group by SUBSTRING(vi_time,1,2)
를 해 주어야 합니다.
접속자 수가 많다면
이런 방법말고
시간대 집계 테이블을 하나 만드시는 것이 좋습니다