2016-11-25 22 views
0

計數博客ID(相同)的no和以降序顯示結果。我在這裏推薦閱讀部分,我需要顯示博客基於無類特定blog.My表的計數是這樣 博客計數博客id並在mysql codeigniter中以降序顯示結果

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 

應該結果在這個format.But我得到只有一條記錄從查詢

這裏是我的代碼:

控制器:

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> 

回答

0
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並顯示結果的正確答案。

1

你正在做一個COUNT(*),但沒有GROUP BY blog_id。

+0

$ this-> db-> select('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(「blog_categories.blog_id」,「DESC」); \t \t $ this-> db-> limit('4,4');添加了這個,但它沒有以正確的順序獲取結果 – user7047368

相關問題