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. 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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.