แบ่งปัน CI-Counter แบบละเอียด Today, Yesterday,This Month, Last Month, This Year, Last Year
พัฒนาต่อจากอันนี้ครับ เอามาเขียนแบบ CI
https://www.thaicreate.com/community/php-counter-day-yesterday-thismonth-lastmonth-year-lastyear.html
1. ในไฟล์ controller ในที่นี่ไว้ใน Home.php
public function index()
{
$date_today = date('Y-m-d');
$last_day = date('Y-m-d',strtotime("-1 day"));
$strSQL = " SELECT DATE FROM counter LIMIT 0,1";
$row_read = $this->db->query($strSQL)->row();
if ($row_read->DATE == $last_day) {
//*** บันทึกข้อมูลของเมื่อวานไปยังตาราง daily ***//
$strSQL1 = " SELECT count(DATE) AS total_yesterday FROM counter WHERE 1 AND DATE = '$last_day'";
$row_count = $this->db->query($strSQL1)->row();
$data = array(
'DATE' => $last_day,
'NUM' => $row_count->total_yesterday
);
if($this->db->insert('daily',$data)){
$condition = array('DATE' => $last_day);
$this->db->delete('counter', $condition);
}
$data_today = array(
'DATE' => $date_today,
'IP' => $_SERVER["REMOTE_ADDR"]
);
$this->db->insert('counter',$data_today);
//$strSQL = " INSERT INTO daily (DATE,NUM) SELECT '".date('Y-m-d',strtotime("-1 day"))."',COUNT(*) AS intYesterday FROM counter WHERE 1 AND DATE = '".date('Y-m-d',strtotime("-1 day"))."'";
//*** ลบข้อมูลของเมื่อวานในตาราง counter ***//
//$strSQL = " DELETE FROM counter WHERE DATE != '".date("Y-m-d")."' ";
}
if($row_read->DATE ==''){
$data_today = array(
'DATE' => $date_today,
'IP' => $_SERVER["REMOTE_ADDR"]
);
$this->db->insert('counter',$data_today);
}
$sdata['count_today'] = $this->get_data->get_counter_today($date_today);
$sdata['count_yesterday'] = $this->get_data->get_counter_yesterday($last_day);
$sdata['count_this_m'] = $this->get_data->get_counter_this_m();
$sdata['count_last_m'] = $this->get_data->get_counter_last_m();
$sdata['count_this_y'] = $this->get_data->get_counter_this_y();
$sdata['count_last_y'] = $this->get_data->get_counter_last_y();
$sdata['count_all'] = $this->get_data->get_counter_all();
//print_r($sdata);
$this->load->view('home',$sdata);
}
2. Model
function get_counter_today($today)
{
$sql = "SELECT COUNT(DATE) AS CounterToday FROM counter WHERE DATE = '$today'";
$row = $this->db->query($sql)->row_array();
return $row;
}
function get_counter_yesterday($lastday)
{
$sql = "SELECT COUNT(DATE) AS Counter_lastday FROM daily WHERE DATE = '$lastday'";
$row = $this->db->query($sql)->row_array();
return $row;
}
function get_counter_this_m()
{
$sql = "SELECT SUM(NUM) AS CountMonth FROM daily WHERE DATE_FORMAT(DATE,'%Y-%m') = '".date('Y-m')."'";
$row = $this->db->query($sql)->row_array();
return $row;
}
function get_counter_last_m()
{
$sql = "SELECT SUM(NUM) AS Count_LastMonth FROM daily WHERE DATE_FORMAT(DATE,'%Y-%m') = '".date('Y-m',strtotime("-1 month"))."'";
$row = $this->db->query($sql)->row_array();
return $row;
}
function get_counter_this_y()
{
$sql = "SELECT SUM(NUM) AS CountYear FROM daily WHERE DATE_FORMAT(DATE,'%Y') = '".date('Y')."' ";
$row = $this->db->query($sql)->row_array();
return $row;
}
function get_counter_last_y()
{
$sql = "SELECT SUM(NUM) AS CountYear FROM daily WHERE DATE_FORMAT(DATE,'%Y') = '".date('Y',strtotime("-1 year"))."' ";
$row = $this->db->query($sql)->row_array();
return $row;
}
function get_counter_all()
{
$sql = "SELECT COUNT(DATE) AS Counter_all FROM daily";
$row = $this->db->query($sql)->row_array();
return $row;
}
3. หน้า views
<table width="100%" class="table table-striped">
<tr>
<td width="98">วันนี้</td>
<td width="75"><div align="center"><?php echo number_format($count_today['CounterToday'],0);?></div></td>
</tr>
<tr>
<td>เมื่อวาน</td>
<td><div align="center"><?php echo number_format($count_yesterday['Counter_lastday'],0);?></div></td>
</tr>
<tr>
<td>เดือนนี้</td>
<td><div align="center"><?php echo number_format($count_this_m['CountMonth']+$count_today['CounterToday'],0);?></div></td>
</tr>
<tr>
<td>เดือนที่แล้ว</td>
<td><div align="center"><?php echo number_format($count_last_m['Count_LastMonth'],0);?></div></td>
</tr>
<tr>
<td>ปีนี้</td>
<td><div align="center"><?php echo number_format($count_this_y['CountYear']+$count_today['CounterToday'],0);?></div></td>
</tr>
<tr>
<td>ปีที่แล้ว</td>
<td><div align="center"><?php echo number_format($count_last_y['CountYear'],0);?></div></td>
</tr>
</table>
สอบถามหรือแสดงความคิดเห็นได้ครับTag : PHP, MySQL, CodeIgniter
Date :
2022-05-07 14:13:49
By :
comsc006
View :
551
Reply :
3
แก้ไข model -> function get_counter_all เป็นแบบนี้ครับ
Code (PHP)
function get_counter_all()
{
$sql = "SELECT SUM(NUM) AS Counter_all FROM daily";
$row = $this->db->query($sql)->row_array();
return $row;
}
Date :
2022-05-09 15:51:41
By :
comsc006
ไม่ควรกำหนดตัวแปรโดยไม่จำเป็น จะทำให้เปลือก Memory
Refactoring
Code (PHP)
function get_counter_all() {
return $this->db->query("SELECT SUM(NUM) AS Counter_all FROM daily")->row_array();
}
Date :
2022-05-12 11:09:33
By :
Guest
Refactor เพิ่มเติม:
Code (PHP)
function get_counter_last_y() {
return $this->db->query("SELECT SUM(`NUM`) AS `CountYear` FROM `daily` WHERE DATE_FORMAT(`DATE`,'%Y') = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR))")->row_array();
}
Date :
2022-05-12 11:16:03
By :
Guest
Load balance : Server 05