如何计算codeigniter博客中的帖子视图

时间:2014-02-03 11:22:36

标签: php codeigniter

我是codeigniter和php的新手。

我想计算发布视图编号并存储数据库。

这意味着,我想通过计算后期视图来创建一个流行的帖子插件。

但我不能这样做。

请有人帮助我。告诉你。

这是我的文章Controller ..

public function __construct(){
    parent::__construct();
    $this->load->model('article_m');
}

public function index($category_slug, $id, $slug=NULL){

    // Fetch the article
    $this->db->where('pubdate <=', date('Y-m-d'));
    $this->data['article'] = $this->article_m->get($id);
    $this->data['articles'] = $this->article_m->get();


    // Return 404 if not found
    count($this->data['article']) || show_404(uri_string());

    if ($this->uri->segment(1) !== $this->data['cat']->category_slug) {
        echo "Hi There, This is wrong";
    }



    $this->load->view('templates/article', $this->data);
}

}

这是我的模特:

     public function get($id = NULL, $single = FALSE){

    if ($id != NULL) {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->where($this->_primary_key, $id);
        $method = 'row';
    }
    elseif($single == TRUE) {
        $method = 'row';
    }
    else {
        $method = 'result';
    }

    if (!count($this->db->ar_orderby)) {
        $this->db->order_by($this->_order_by);
    }
    return $this->db->get($this->_table_name)->$method();
}

抱歉我的英文不好..

2 个答案:

答案 0 :(得分:0)

$post_id = "???"
if(isset($_COOKIE['read_articles'])) {
  //grab the JSON encoded data from the cookie and decode into PHP Array
  $read_articles = json_decode($_COOKIE['read_articles'], true);
   if(isset($read_articles[$post_id]) AND $read_articles[$post_id] == 1) {
     //this post has already been read
   } else {

     //increment the post view count by 1 using update queries

     $read_articles[$post_id] = 1;
     //set the cookie again with the new article ID set to 1
     setcookie("read_articles",json_encode($read_articles),time()+60*60*24);  

   }

} else {

  //hasn't read an article in 24 hours?
  //increment the post view count
  $read_articles = Array(); 
  $read_articles[$post_id] = 1;
  setcookie("read_articles",json_encode($read_articles),time()+60*60*24);      

}

答案 1 :(得分:0)

明星复杂吗?

//function single post, query get by slug and id post


        if(!isset($_SESSION['views'. $id]) || (isset($_SESSION['views'. $id]) && $_SESSION['views'. $id] != $id)){
            $this->post_m->viewer($id); //load model viewer
            $this->session->set_userdata('views'. $id, $id);
        }

// Model "post_m"
function viewer($id)
{
    $this->db->where('id', $id); 
    $this->db->set('views', 'views+1', FALSE);
    $this->db->update('posts'); //table update
} 


$id is id post

相关问题