[英]counting the blog ids and displaying the results in descending order in mysql codeigniter
计算博客ID的数量(相同)并以降序显示结果。我建议阅读这里的部分,我需要基于特定博客的类别数量来显示博客。我的表格如下所示
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 count是3,对于1来说,count是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>
您正在执行COUNT(*),但没有GROUP BY 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并显示结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.