计数博客ID的否(相同)并以降序显示结果。我在此处推荐读取部分,我需要根据" no of类别的conter"来显示博客一个特定的博客。我的桌子看起来像这样博客
blog_id| image_path | description
-------------------------------------------
1 | image.png | description
2 | image1.png | description
3 | image2.png | description
4 | image3.png | description
blog_categories
blog_category_id | blog_id | category_id
-------------------------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
4 | 3 | 4
5 | 3 | 2
6 | 3 | 6
在blog_categories表中表blog_id 3计数为3,对于1,计数为2,因此在显示结果时,第一个应该为
blog_id
3
1
2
应该以这种格式的结果。但是我只从查询中获得一个记录
这是我的代码:
控制器:
public function article()
{
$this->load->model('blogs_model');
$data['records4'] = $this->blogs_model->get_all_recommended();
$data['mainpage']='blogs';
$this->load->view('templates/templatess',$data);
}
模型:
function get_all_recommended()
{
$this->db->select('count(*),image_path,description');
$this->db->from('blog_categories');
$this->db->join('blogs AS B','B.blog_id=blog_categories.blog_id','INNER');
$this->db->order_by("blog_categories.blog_id", "DESC");
$this->db->limit('4,4');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return false;
}
}
查看:
<?php if(isset($records4) && is_array($records4)):?>
<?php foreach ($records4 as $r):?>
<div class="clearfix float-my-children">
<img src="<?php echo base_url();?>admin/images/blogimages/thumbs/<?php echo $r->image_path;?>" width=100>
<div class="blogclasstext134"><?php echo $r->blog_text;?></div>
</div>
您正在做一个计数(*),但没有blog_id的组。
function get_all_recommended()
{
$this->db->select('B.blog_id,count(*),image_path,blog_title');
$this->db->from('blog_categories');
$this->db->join('blogs AS B','B.blog_id=blog_categories.blog_id','INNER');
$this->db->join('categories AS C','C.category_id=blog_categories.category_id','INNER');
$this->db->group_by('B.blog_id');
$this->db->order_by("count(blog_categories.blog_id)", "DESC");
$this->db->limit('4,4');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return false;
}
}
正确计算blog_ids并显示结果的答案。