FV = $this->load->database('mysql', TRUE); } /** * 重置查询 */ private function reset() { $this->h_id = false; $this->h_visit_id = false; $this->h_page_id = false; $this->h_datetime = false; $this->my_fields = false; $this->order_by = false; $this->limit = false; } /** * 获取访客数据 * @param type $key */ public function add($v_id, $page_id, $page_referer = '') { if ($v_id AND $page_id) { $sql = "INSERT INTO analytics_historys ( h_visit_id, h_page_id, h_page_referer, h_datetime ) VALUES ( ?, ?, ?, ? )"; return $this->FV->query($sql, array($v_id, $page_id, $page_referer, time())); } return FALSE; } /** * 获取指定访客的访问记录 * @param type $lv_id */ public function get_visit_history($v_id) { if (!empty($v_id)) { // LEFT JOIN, 在history表有lv_id索引的情况下 1KW数据 查询0.1s左右, // TODO: 有时间可以该表到2KW数据以上SQL查询的效率(查询应该也很快, 但是要试试看). $sql = " SELECT h.h_id, h.h_visit_id, h.h_page_id, h.h_page_referer, h.h_datetime, p.p_url, p.p_author FROM analytics_historys h LEFT JOIN analytics_pages p ON p.p_id = h.h_page_id WHERE h_visit_id = $v_id ORDER BY h_id"; $query = $this->FV->query($sql); return $query->result(); } return FALSE; } /** * 根据传入的访客ID数组获取访问次数 * @param type $visit_ids */ public function get_visit_count($visit_ids = array()) { if (is_array($visit_ids) && count($visit_ids) > 0) { $this->reset(); $value = '(' . implode(",", $visit_ids) . ')'; $this->h_visit_id = ' AND h_visit_id IN ' . $value; $this->my_fields = 'count(h_id) c'; $this->limit = 1; $result = $this->get_list(); if (!empty($result)) { return $result->c; } } return 0; } /** * 获取数据集 */ private function get_list() { $sql = "SELECT "; if ($this->my_fields === false) { $sql .= "h_id," . " h_visit_id," . " h_page_id," . " h_datetime"; } else { $sql .= $this->my_fields; } $sql .= " FROM analytics_historys"; $sql .= " WHERE 1=1 "; $this->h_id ? $sql .= $this->h_id : false; $this->h_visit_id ? $sql .= $this->h_visit_id : false; $this->h_page_id ? $sql .= $this->h_page_id : false; $this->h_datetime ? $sql .= $this->h_datetime : false; $this->order_by ? $sql.=$this->order_by : false; $this->limit ? $sql.= ' LIMIT ' . $this->limit : false; $query = $this->FV->query($sql); if ($this->limit == 1) { if ($query->num_rows() > 0) { $row = $query->row(); return $row; } else { return FALSE; } } else { return $query->result(); } } }