我正在根据codeigniter blog tutorial指令构建一个简单的博客帖子和评论。现在我正在尝试使用jquery ajax在一个页面上实现一个facebook-wall-comment,我已经设法做了墙贴,每个墙上的评论帖,也将评论分组到相应的帖子,但我有显示每个墙的评论计数的问题,我想做像这样的事情
型号:
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
。
我希望显示类似
的内容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 :(得分:2)
您不应该使用count()
来计算评论数!只需使用为此目的创建的num_rows()
计算返回的行。
例如:
<?php
$count_comments = $this->db->query("SELECT * FROM comments WHERE post_id = ".$post_row['id']);
echo $count_comments->num_rows();
?>