2011-01-08 101 views
2

我剛開始我的第一個CI應用程序。我有一個顯示一些帖子的視圖。每篇文章可以有多個評論,我希望顯示每篇文章旁邊的評論總數。CodeIgniter:幫助這個查詢

到目前爲止,我所有的數據庫調用都在我的控制器(將改變這一點)。

function index(){ 
    $data['query'] = $this->db->get('posts'); 
    $this->load->view('blog_view', $data); 
} 

筆者認爲:

<?php foreach($query->result() as $row): 
     <div class="post-box"> 
      <p><?php echo $row->body; ?><small>&nbsp;added by <?php echo $row->username; ?> on <?php echo date ('d/m/Y',strtotime($row->created)); ?>&nbsp;<a href="<?php echo base_url(); ?>blog/comments/<?php echo $row->id; ?>"><img src="<?php echo base_url(); ?>images/comments_icon.png" />&nbsp;0</a></small></p> 
     </div> 
<?php endforeach; ?> 

我想要得到的意見,其中comment.post_id =當前記錄的ID總數。並將其顯示在評論圖標旁邊。

與此最爲欣賞的任何幫助,

比利

UPDATE

控制器:

function index(){ 
    //load the model 
    $this->load->model('City_model'); 

    //call the model method 
    $data->posts = $this->City_model->get_posts(); 


    $this->load->view('blog_view', $data); 
} 

模型(city_model.php):

<?php 

class City_model extends Model{ 

    function get_posts($id = NULL) { 

     //define optional id for single post 
     //if an id was supplied 
     if ($id != NULL) { 
      $this->db->where('id',$id); 
     } 

     // execute query 
     $query = $this->db->get('posts'); 

     //make sure results exist 
     if($query->num_rows() > 0) { 
      $posts = $query->result(); 
     } else { 
      return FALSE; 
     } 

     //create array for appended (with comments) posts 
     $appended_posts_array = array(); 

     //loop through each post 
     foreach ($posts as $post) { 

      //get comments associated with the post 
      $this->db->where('post_id', $post->id) 
      $comments = $this->db->get('comments'); 

      //if there are comments, add the comments to the post object 
      if($comments->num_rows() > 0) { 
       $post->comments = $comments; 
      } 
      else { 
       $post->comments = array(); 
      } 

      //rebuild the returned posts with their comments 
      $appended_posts_array[] = $post; 

     } 

     //if post id supplied, only return the single post object 
     if ($id != NULL) { 
      return $appended_registration_array[0]; 
     } 
     else { 
      return $appended_registration_array; 
     } 
    } 
} 

回答

3

這裏是,所採用的方法,我通常對於有「亞健康」筆數做榜樣模型的方法...

我一般建立他們在像這樣的模型多級陣列...

注意:此模型返回一篇文章或全部文章,其中包含一系列可通過->comments屬性訪問的關聯評論。

function get_posts($id = NULL) { 

    //define optional id for single post 
    //if an id was supplied 
    if ($id != NULL) { 
     $this->db->where('id',$id); 
    } 

    // execute query 
    $query = $this->db->get('posts'); 

    //make sure results exist 
    if($query->num_rows() > 0) { 
     $posts = $query->result(); 
    } else { 
     return FALSE; 
    } 

    //create array for appended (with comments) posts 
    $appended_posts_array = array(); 

    //loop through each post 
    foreach ($posts as $post) { 

     //get comments associated with the post 
     $this->db->where('post_id', $post->id) 
     $comments = $this->db->get('comments'); 

     //if there are comments, add the comments to the post object 
     if($comments->num_rows() > 0) { 
      $post->comments = $comments; 
     } 
     else { 
      $post->comments = array(); 
     } 

     //rebuild the returned posts with their comments 
     $appended_posts_array[] = $post; 

    } 

    //if post id supplied, only return the single post object 
    if ($id != NULL) { 
     return $appended_registration_array[0]; 
    } 
    else { 
     return $appended_registration_array; 
    }  
} 
控制器

現在...

function posts() { 

    //load the model 
    $this->load->model('model_name'); 

    //call the model method 
    $data->posts = $this->model_name->get_posts(); 

    //load the view 
    $this->load->view('view/file', $data); 

} 

然後在視圖中,您可以通過帖子和評論

<? foreach($posts as $post): ?>    //posts foreach start 

    <h1><?= $post->title ?></h1> //post title 
    <p><?= $post->body ?></p>  //post body 

    <? foreach($post->comments as $comment): ?>  //comments foreach start  
     <h3><?= $comment->author ?></h3> //comment author 
     <p><?= $comment->body ?></h3>  //comment body 
    <? endforeach; ?>        // end comments foreach 

<? endforeach; ?>  // end posts foreach 

拿筆記使用嵌套的foreach循環是一旦你構建了像我在模型中顯示的posts數組,對於每個$ post項目,你都有一個$ post-> comments,它只是與該帖子相關聯的註釋數組,所以knowi除此之外,您可以在控制器或視圖中調用count($post->comments)以獲取與單個帖子相關的評論數量。

所以你對只顯示計數,在視圖問題,唯一的變化是,你將通過所有的評論不循環,你只是做到這一點,而不是...

<? foreach($posts as $post): ?>    //posts foreach start 

    <h1><?= $post->title ?></h1>    //post title 
    <p><?= count($post->comments) ?></p>  //comment count 


<? endforeach; ?>  // end posts foreach 

編輯: 我將可選參數$id添加到模型方法,以便如果你想要,你可以指定一個單一的職位,如果你願意,通過傳遞它的ID到方法。這樣,同樣的模型方法可以重新用於顯示單個帖子,並顯示所有註釋。

+0

@jondavidjohn:我刪除了原來的答案,因爲你的答案更全面,更多的是MVC的「精神」,所以說。我同意OP應該通過「快速修復」選擇這種類型的解決方案。「+1抽出時間來說明正確的做法 – 2011-01-08 21:11:40