开发者

codeigniter passing some id to display a comment count for a pasticular post

i am building a simple blog posting and commenting following the codeigniter blog tutorial instruction. Now i am trying to implement a facebook-wall-comment like on one page using jquery ajax, i have manage to do the wall post, and the comment post on each wall, also grouping the comments into its corresponding post, but i am having problem displaying the comment count for each wall, i am thinking like doing something like this

Model:

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

Controller:

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

how should i pass this $post_id parameters from my view (all i can think of for now)

since my post controller is this:

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

and the only way i can fetch the particular id for each post is by looping the $post_query in my view.

i want to display something like

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_po开发者_开发百科st($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))

please help me on implementing the "best practice" way to do this, which is: what should i write on my model, my controller and my view.

best regards


You should not be using count() to count the comments! Just count the returned rows using num_rows() which was created for that purpose.

eg:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜