我正在建立一个简单的博客张贴和评论遵循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参数(目前我所能想到的全部)
因为我的post控制器是:
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))请帮助我实现“最佳实践”的方式,即:我应该在我的模型、控制器和视图上写些什么。
诚挚的问候
发布于 2011-03-22 20:51:57
你不应该使用count()来统计评论!只需使用为此目的而创建的num_rows()计算返回的行数。
例如:
<?php
$count_comments = $this->db->query("SELECT * FROM comments WHERE post_id = ".$post_row['id']);
echo $count_comments->num_rows();
?>https://stackoverflow.com/questions/5389835
复制相似问题