簡體   English   中英

計數博客ID並在mysql codeigniter中按降序顯示結果

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM