2011-03-22 1 views
1

나는 codeigniter blog tutorial 명령어를 따라 간단한 블로그 게시 및 코멘트를 작성하고 있습니다. 이제는 jquery ajax를 사용하여 한 페이지에서 페이스 북 - 벽 - 주석을 구현하려고합니다. 벽 포스트를 관리하고 각 벽에 댓글을 게시하고 해당 게시물에 댓글을 그룹화합니다. 각 벽에 대한 코멘트 수를 표시하는 문제는,이cordigniter는 신전 포스트에 대한 코멘트 카운트를 표시하기 위해 일부 ID를 전달합니다.

모델과 같은 일을하고 같은 생각입니다 :

function count_for_post($post_id){ 
    $query = $this->db->query("SELECT COUNT(id) FROM comments WHERE post_id = ".$post_id); 
    return $query; 
} 

컨트롤러 :

function comments(){ 
    $data['comment_query'] = $this->commentmodel->count_for_post($post_id); 
    $this->load->view('content/post/comment_view', $data); 
} 

어떻게 내가 내보기에서이 $post_id 매개 변수를 전달한다 (내가 아무 것도 생각할 수 없다.

function index(){ 
    $data['post_query'] = $this->postmodel->get_desc(); 
    $this->load->view('content/postview', $data); 
} 

와 나는 각 게시물에 대한 특정 ID를 가져올 수있는 유일한 방법은 내보기에 $post_query을 반복하는 것입니다 : 내 게시물 컨트롤러 이후 w)는

이있다. 난 내 모델 내 컨트롤러와 내보기에 작성해야 무엇 :

나는 인이 작업을 수행하기 위해 "최선의 방법"방법을 구현하는 저를 도와주세요

contents of Post 1 
    contents of comment regarding post1 
    contents of comment regarding post1 
    contents of comment regarding post1 

    view all count($this->commentmodel->count_for_post($post_id)) 

contents of Post 2 
    contents of comment regarding post2 
    contents of comment regarding post2 
    contents of comment regarding post2 

    view all count($this->commentmodel->count_for_post($post_id2)) 

contents of Post 3 
    contents of comment regarding post3 
    contents of comment regarding post3 
    contents of comment regarding post3 

    view all count($this->commentmodel->count_for_post($post_id3)) 

같은 것을 표시 할.

안부

+0

'복귀 카운트를하지 않는다 카운트를하고 ($ 질의 -> result_array());'. 대신에 MySQL 수를 – JohnP

+0

오, 고마워 존, 내가 그걸 수정합니다 – littlechad

+0

내가 선호하는 질문이라고 생각 - 하나는 당신이 PHP에서 계산을 할 수있을 때 DB를 쿼리해야한다고 생각하지 않는다, 그것은 빠르고 적은 리소스 태워 버리는. 나는 단순히'$ data' 변수에서보기에있는 게시물의 수를 계산했을 것입니다. – BigFatBaby

답변

2

당신은 코멘트를 계산 count()를 사용해서는 안됩니다! 그냥 그 목적을 위해 만들어진 num_rows()을 사용하여 반환 된 행을 계산하십시오.

예 :

<?php 
    $count_comments = $this->db->query("SELECT * FROM comments WHERE post_id = ".$post_row['id']); 
    echo $count_comments->num_rows(); 
?> 
+0

내보기 안에 이것을 작성하는 것이 괜찮습니까 ?? – littlechad

+0

아니,보기는 단지 프레 젠 테이션을 위해, 당신은 함수를 (예 : commentcount)을 만들어야하고 대신 echo'ing 카운트를 그냥 그것을 반환합니다 – Christophe