首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >codeigniter传递一些id来显示pasticular帖子的评论计数

问codeigniter传递一些id来显示pasticular帖子的评论计数
EN

Stack Overflow用户
提问于 2011-03-22 18:29:05
回答 1查看 1.6K关注 0票数 1

我正在建立一个简单的博客张贴和评论遵循codeigniter blog tutorial的指令。现在我正在尝试使用jquery ajax在一个页面上实现facebook- wall -comment,我已经设法完成了墙上的帖子,以及每面墙上的评论帖子,也将评论分组到相应的帖子中,但我在显示每面墙的评论计数时遇到了问题,我想这样做

型号:

代码语言:javascript
复制
function count_for_post($post_id){
    $query = $this->db->query("SELECT COUNT( id ) FROM comments WHERE post_id = ".$post_id);
    return $query; 
}

控制器:

代码语言:javascript
复制
function comments(){
    $data['comment_query'] = $this->commentmodel->count_for_post($post_id);
    $this->load->view('content/post/comment_view', $data);
}

我应该如何从我的视图传递这个$post_id参数(目前我所能想到的全部)

因为我的post控制器是:

代码语言:javascript
复制
function index(){
    $data['post_query'] = $this->postmodel->get_desc();
    $this->load->view('content/postview', $data);
}

我可以获取每个帖子的特定id的唯一方法是在我的视图中循环$post_query。

我想要显示如下内容

代码语言:javascript
复制
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))

请帮助我实现“最佳实践”的方式,即:我应该在我的模型、控制器和视图上写些什么。

诚挚的问候

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-22 20:51:57

你不应该使用count()来统计评论!只需使用为此目的而创建的num_rows()计算返回的行数。

例如:

代码语言:javascript
复制
<?php 
    $count_comments = $this->db->query("SELECT * FROM comments WHERE post_id = ".$post_row['id']);
    echo $count_comments->num_rows();
?>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5389835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档