我正在一个论坛上工作,并且我想要跟踪页面浏览量。
我决定在提取线程内容(OP)的mysql查询之后添加视图更新代码。
public function get_threads($threadid)
{
$sql = "SELECT t.*, u.username, up.avatar_loc from threads t
INNER JOIN users u
on u.id = t.userid
INNER JOIN user_profiles up
on u.id = up.id
where threadid = $threadid";
$result = $this->db->query($sql)->row_array();
$view = $result['numviews'];
$view ++;
$update = "UPDATE threads SET numviews = $view WHERE threadid = $threadid";
$this->db->query($update);
return $result;
}
但是,在Chrome中,页面刷新时numviews
值会增加两倍。这不会发生在Firefox中。
通常,内容将通过AJAX调用加载,当它通过这种方式时,它也没有任何问题。在AJAX调用之后,我使用popstate更改URL,如果用户在页面上刷新,则会出现错误,或者如果他们导航到页面(使用在新窗口中打开)。
以下是AJAX和非ajax的控制器代码:
非Ajax线程控制器:
public function threads($threadid)
{
//...whole lot of other stuff
$data['content'] = $this->components_m->thread($threadid);
$this->load->view('template/template' ,$page);
}
AJAX控制器:
public function ajax_thread()
{
$threadid = $this->input->post('threadid');
$result['content'] = $this->components_m->thread($threadid);
echo json_encode($result);
}
以下是components_m->thread($threadid)
public function thread($threadid)
{
$data['threadid'] = $threadid;
$data['wysihtml5_toolbar'] = $this->load->view('newthread/wysihtml5_toolbar','',TRUE);
$data['op'] = $this->threads_m->get_threads($threadid);
$data['replies'] = $this->threads_m->replies($threadid);
$page['subject'] = $data['op']['subject'];
$page['replyTo'] = $this->threads_m->replyTo($data);
$page['replies'] = $this->threads_m->create_replies($data);
return $this->load->view('thread', $page, TRUE);
}
我搜索了整个项目,get_threads($threadid)
仅存在于上面列出的两个函数中。
当我使用$this->output->enable_profiler(TRUE);
时,我只看到运行一次的更新功能。
我甚至尝试在$result['content'] = $this->components_m->thread($threadid);
评论ajax_thread()
,但在Chrome中仍然会更新两次。
有什么想法吗?
答案 0 :(得分:0)
通过更改UPDATE sql代码来修复它:
$update = "UPDATE threads SET numviews = numviews + 1 WHERE threadid = $threadid";
$this->db->query($update);