<?php
session_start();
$todayc = date('Y-m-d');
$db = new mysqli('localhost','DB아이디','DB비번','DB테이블');
if(mysqli_connect_errno()){exit("error");}

//오늘날짜의 db가 있는지 찾는다
$query = "select * from today where date='$todayc'";
$result = $db->query($query);
//db에 오늘날짜가 잇다면$result_row에 갯수 대입
$result_row = $db->affected_rows;

//오늘이아니라면
if($result_row == 0)
{
   $_SESSION['today'] = "1";
   //db에 오늘날짜와 카운터를 1로 맞춘다
   $query = "insert into today values('','$todayc','1')";
   $result = $db->query($query);
}
//오늘이고 처음방문자라면
if($result_row ==1 && $_SESSION['today'] != "1")
{
   //세션설정
   $_SESSION['today'] = "1";
   //오늘db에 count+1한다
   $query = "update today set count=count+1 where date='$todayc'";
   $result = $db->query($query);
}
?>
<table border=1 width=300 style="display:none;">
   <tr>
      <td>날짜</td>
      <td>접속수</td>
   </tr>
   <?php
   //날짜와 접속수 출력
   $query="select * from today order by id desc";
   $result=mysqli_query($db,$query);
   while($data=$result->fetch_array())
   {
      ?>
      <tr>
         <td><?=$data['date']?></td>
         <td><?=$data['count']?></td>
      </tr>
      <?php
   }

   //전체 방문자 구하기
   $query="select * from today order by id desc";
   $result=mysqli_query($db,$query);
   while($data=$result->fetch_array())
   {
      $total += $data[count];
   }
   echo "<br><tr><td colspan=2>총 ".$total."명</td></tr>";
   ?>
</table>
<!--

//if ($now == $last) {$data[today]++;} //오늘방문자수 구하기

//출처: https://pram.tistory.com/entry/예제mysql을-이용한-카운터 [프로그램공부]
//$query ="SELECT * FROM today where date like '2019-01-21'";

//$query = "select count(*) as count from tb_stat_visit where DATE(regdate) = DATE('$currdt')";
//$result=mysqli_query($db,$query);
//출처: https://thereclub.tistory.com/56 [아메리카노 공방]
-->

<?php
$now = date('Y-m-d');
$query = "select * from today where DATE = '$now'";
$data = $db->query($query)->fetch_array();
$today = $data[count];
?>

 


<include target="./assets/inc/count2.php" />

레이아웃에서 위 파일을 인클루드한 뒤에,

$today 변수를 레이아웃에 출력하고 싶습니다. -_-;

어떻게 방법이 없을까요

 

소스출처 

https://m.blog.naver.com/PostView.nhn?blogId=gogoke1&logNo=20197675282&proxyReferer=https%3A%2F%2Fwww.google.com%2F

기타참조

https://doorbw.tistory.com/22

https://thereclub.tistory.com/56

  • profile
    파일 하나에 쓰고 그 파일을 인클루드하는 방법으로 해결할 수 있을거같네요 ㅠㅠ
  • profile
    인클루드한 파일에서 Context::set('today', $data[count]); 라고 하면 레이아웃에서 $today로 불러올 수 있을 것 같네요. XE에서 인클루드하면 session_start()는 필요없으니 지우시고요.
  • profile profile
    마이 히어로! ㅠㅠ)/;;
    감사합니다.