Php codeigniter传递一些id以显示粘贴帖子的注释计数
我正在建立一个简单的博客张贴和评论以下的指示。现在我正在尝试使用jquery ajax在一个页面上实现facebook墙评论,我已经成功地完成了墙帖子,以及每面墙上的评论帖子,也将评论分组到了相应的帖子中,但是我在显示每面墙的评论数时遇到了问题,我想这样做 型号:Php codeigniter传递一些id以显示粘贴帖子的注释计数,php,codeigniter,parameter-passing,Php,Codeigniter,Parameter Passing,我正在建立一个简单的博客张贴和评论以下的指示。现在我正在尝试使用jquery ajax在一个页面上实现facebook墙评论,我已经成功地完成了墙帖子,以及每面墙上的评论帖子,也将评论分组到了相应的帖子中,但是我在显示每面墙的评论数时遇到了问题,我想这样做 型号: function count_for_post($post_id){ $query = $this->db->query("SELECT COUNT( id ) FROM comments WHERE post_i
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\u id
参数(我现在所能想到的)
因为我的邮政管理员是这样的:
function index(){
$data['post_query'] = $this->postmodel->get_desc();
$this->load->view('content/postview', $data);
}
我能为每篇文章获取特定id的唯一方法是在我的视图中循环$post\u query
我想展示像这样的东西
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))
请帮助我实现“最佳实践”方法,即:我应该在模型、控制器和视图上写些什么
致以最诚挚的问候您不应该使用
count()
来计算评论!只需使用为此目的而创建的num_rows()
对返回的行进行计数
例如:
进行计数时不要返回计数($query->result_array())代码>。改为MySQL count噢,谢谢john,我将修改这一点,我认为这是一个偏好的问题-我不认为当你可以用php进行计算时,你应该查询你的数据库,它更快、更少地消耗资源。我会简单地从$data
变量中计算视图中的帖子数量。我是否可以在视图中编写此内容?否,视图仅用于演示,您必须创建一个函数(例如:commentcount),而不是回显计数,只需返回它即可
<?php
$count_comments = $this->db->query("SELECT * FROM comments WHERE post_id = ".$post_row['id']);
echo $count_comments->num_rows();
?>