我尝试根据当前日期和月份添加ip,并以json的形式显示它,但在我尝试ip后无法添加。我的剧本缺少什么?
示例数据
| 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 |
控制器
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);
}
模型
function get_data() {
$sql = "SELECT count(ip) as total FROM my_table WHERE DATE(date) = date('d') ";
return $this->db->query($sql)->result_array();
}
结果
[{"date_on_month":"01","total":"0"},{"date_on_month":"02","total":"0"},{"date_on_month":"03","total":"0"} .........]
答案 0 :(得分:1)
您的控制器将如下:
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);
}
您的模型应该是:
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();
}