2014-01-07 2 views
0

안녕하세요 저는 codeigniter 프레임 워크를 기반으로 포스트 카운트 기능을 만들기 위해 노력하고 있습니다. 함수가 완료되고 모든 것이 잘 작동합니다.Codeigniter 포스트 뷰 카운트 기능 -

하지만 여기에 몇 가지 전문가의 질문이 있습니다.이 질문은 기능의 올바른 사용법이고 위의 항목 중 하나가 내 페이지 로딩에 해를 끼치 지 않는다고 말해줍니다. 이 함수는 게시물에 대한 모든 클릭에 대해 jQuery get 함수에 의해 액세스됩니다.

먼저 IP 주소가 있는지 확인하고 삽입 된 IP 주소가 24 시간 이상이면 현재 행을 삭제하고 다시 이전 선택을 확인합니다. 마지막 IP 주소와 새로운 날짜로 다시 삽입.

그리고 다른 질문은 내가 매주 또는 모든 IP addreses에 대한 청소를해야합니까?

function show_count($post_id){ 

     $ip = $this->input->ip_address(); 

      $this->db->select('ip_address,data'); 
      $this->db->from('post_views'); 
      $this->db->where('ip_address',$ip);  
      $query = $this->db->get(); 

       foreach ($query->result() as $row){ 
        $ip_address = $row->ip_address;     
        $data = $row->data;     
       } 

     if(isset($ip_address) && time() >= strtotime($data) + 8640){ 

      $this->db->where('ip_address',$ip); 
      $this->db->delete('post_views'); 
     } 

      $this->db->select('ip_address'); 
      $this->db->from('post_views'); 
      $this->db->where('ip_address',$ip);  
      $query = $this->db->get(); 

       foreach ($query->result() as $row){ 
        $ip_address_new = $row->ip_address;       
       } 

     if(!isset($ip_address_new) && $ip_address_new == false){ 

      $date = new DateTime('now', new DateTimeZone('Europe/Skopje')); 
      $this->db->set('views', 'views+ 1', false); 
      $this->db->where('post_id',$post_id); 
      $this->db->update('posts'); 

      $data = array(
       'ip_address'=>$ip, 
       'data'=>$date->format("Y-m-d H:i:s") 
       ); 

      $this->db->insert('post_views',$data); 
     } 

    } 

감사합니다, 어떤 제안은 감사합니다 :

여기 내 코드입니다.

+0

안녕하세요, 저는 더 많이하고있는 것을 이해하기 위해 노력하고 있습니다. 글을 읽었을 때 고유 한 의견을 기록에 남기고 싶습니까? –

+0

아니요, 게시물 조회수를 계산하는 조회수 계산 기능을 만들고 있지만 페이지를 새로 고칠 때 또는 여러 번 클릭 할 때보기 값을 증가시키지 않습니다. – user2317735

+0

좋아요, 답장에서해야 할 일을 제안하겠습니다. 당신이 묘사 한 것은 독특한 견해입니다 ... –

답변

1

게시물의 고유 한보기를 증가시키기 위해 많은 쿼리를 수행하는 대신 쿠키를 사용 및 설정하고 쿠키가 활성화되지 않은 경우 대체 방법을 사용해야합니다.

$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);  

} 
+0

그레이트 아이디어. 나는 이것을 시도 할 것이고 여기에 결과를 게시 할 것이다. 고마워 해리 – user2317735

+0

아무 문제가 있으면 아무 문제도 알려주세요 :) –

+0

Btw, 이미 내 로그인 시스템 해리 용 쿠키를 사용하고 있습니다. 이것이 문제가 될까요? – user2317735