2016-11-25 4 views
0

ブログIDの数を数えて(同じ)、降順で結果を表示しています。私はここで読んだセクションをお勧めしています。私はブログに基づいてブログを表示する必要があります特定の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テーブルに3カウントが3であるblog_idと1のカウントは、結果を表示しながら、それが結果はこのformat.Butで私は1つのレコードだけを取得していなければならない

blog_id 
3 
1 
2 

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> 

答えて

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(*)が、blog_id BYなしGROUPを行っています。

+0

$ this-> db-> select( 'count(*)、image_path、blog_title'); $ this-> db-> from( 'blog_categories'); $ this-> db-> join( 'ブログAS B'、 'B.blog_id = blog_categories.blog_id'、 'INNER'); $ this-> db-> join( 'カテゴリ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

関連する問題