簡體   English   中英

一個月中每天無法添加基於IP的訪問者

[英]Can not add ip based visitors per day in a month

我嘗試根據當前日期和月份添加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"} .........]

您的控制器將為:

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();  
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM