dqtok88424 2011-03-22 10:29
浏览 44
已采纳

codeigniter传递一些id来显示特定帖子的评论计数

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_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))

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

  • 写回答

1条回答 默认 最新

  • dopnpoh056622 2011-03-22 12:51
    关注

    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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等