dousi1097 2016-11-25 12:46
浏览 38
已采纳

计算博客ID并在mysql codeigniter中以降序显示结果

Counting the no of blog ids(same) and displaying the results in descending order.I am having recommended read section here i need to display the blogs based on the count of no of categories for a particular blog.My table looks like this blogs

    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

Here in blog_categories table blog_id 3 count is 3 and for 1 the count is 2 so while displaying the the results the first one should be

blog_id
3
1
2

It should the result in this format.But i am getting only one record from the query

Here is my code:

Controller:

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);        
    }

Model:

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;
    } 
}

View:

<?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>
  • 写回答

2条回答 默认 最新

  • dqsxsmi3704 2016-11-28 06:21
    关注
    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;
        } 
    }
    

    Correct answer for counting the blog_ids and displaying the results.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看