[英]Can not add ip based visitors per day in a month
I tried to add ip based on the current date and month, and displays it in the form of json, but after I try ip can not be added. 我尝试根据当前日期和月份添加ip,并以json的形式显示它,但是在尝试ip之后无法添加。 what is lacking in my script? 我的脚本缺少什么?
sample data 样本数据
| date | IP
===============================
| 2015-02-01 | 10.88.25.139 |
| 2015-02-02 | 10.88.25.138 |
| 2015-02-03 | 10.88.25.130 |
| 2015-02-03 | 10.88.25.131 |
| 2015-02-03 | 10.88.25.132 |
| 2015-02-04 | 10.88.25.139 |
| 2015-02-04 | 10.88.25.138 |
| 2015-02-05 | 10.88.25.131 |
| 2015-02-06 | 10.88.25.131 |
| 2015-02-06 | 10.88.25.138 |
| 2015-02-06 | 10.88.25.139 |
Controller 控制者
public function get_date_statistik() {
for($i = 1; $i <= date('t'); $i++) {
foreach($this->my_model->get_data() as $row)
{
$data[] = array(
'date_on_month' => str_pad($i, 2, '0', STR_PAD_LEFT),
'total' => $row['total']
);
}
}
echo json_encode($data);
}
Models 楷模
function get_data() {
$sql = "SELECT count(ip) as total FROM my_table WHERE DATE(date) = date('d') ";
return $this->db->query($sql)->result_array();
}
Result 结果
[{"date_on_month":"01","total":"0"},{"date_on_month":"02","total":"0"},{"date_on_month":"03","total":"0"} .........]
Your controller will be as : 您的控制器将为:
public function get_date_statistik() {
for($i = 1; $i <= date('t'); $i++) {
$date_string = date('Y')."-".date('m')."-".$i;
foreach($this->my_model->get_data($date_string) as $row)
{
$data[] = array(
'date_on_month' => str_pad($i, 2, '0', STR_PAD_LEFT),
'total' => $row['total']
);
}
}
echo json_encode($data);
}
Your model should be like: 您的模型应类似于:
function get_data($date_string) {
$sql = "SELECT count(ip) as total FROM my_table DATE_FORMAT( `date` , '%m-%d' ) = DATE_FORMAT($date_string, '%m-%d' )";
return $this->db->query($sql)->result_array();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.