2014-01-23 76 views
0

目前我有一個博客系統的問題。用戶將發佈他們的博客和其他成員可以發表評論。當用戶打開自己的家,他頁/她能看到他寫的/她附有每個評論的博客blog.like這 -博客和評論問題在Codeigniter

博客1 .Comment1 .comment2 ...

博客2 .Comment1 .comment2 .Comment3 ....

等。筆者對本 1.博客兩個表_tbl(blg_id,blg_title,blg_content,author_id,crt_date)2.comments_tbl(cmmnt_id,blg_id,cmnt_txt,author_id,crt_date)

現在我可以顯示所有的博客。但是在顯示針對特定博客的評論時遇到問題。 要提取這些註釋我的步驟 -

  • 我有一個數組$ blog_ids其中包含最新的5個博客的ID。
  • 傳遞$ blog_ids我的模型

代碼:

public function get_comments($blog_ids) 
{ 
    foreach($blog_ids as $row) 
    { 

     $blg_post_id = $row['blg_id']; 
     $this->db->where('post_id', $blg_post_id); 
     $get_comments = $this->db->get('comment_tbl'); 
     $cmnts = $get_comments->result_array(); 

    } 
    return $cmnts;   
} 
// end of get_comments 
  • 現在我通過這個$ cmnts陣列結果的陣列,以我的看法。

  • 在這裏的觀點我不能夠區分評論關於員額

首先$ comnts的持有混了每一blogs.How到discriminate.Secondly所有評論中, foreach循環我做錯了什麼,我沒有顯示任何東西。是因爲它的數組數組?

<ul class="cmmnt"> 
<?php foreach($comnts as $value){ ?> 
<li> 
<div class=cmnt_container> 
<div class=commnt_txt> 
<span class="h5"><?php echo $value['comment_txt'] ;?></span> 
</div> 
</div> 
</ul> 
+0

你總是覆蓋評論陣列,而不是你必須將其追加 – Sundar

回答

0

你把所有的意見在同一個變量$ cmnts。你可以試試這個:

public function get_comments($blog_ids) 
    { 
     foreach($blog_ids as $row) 
     { 
     // First declare $cmnts as array 
     $cmnts = array(); 
     $blg_post_id = $row['blg_id']; 

     $this->db->where('post_id', $blg_post_id); 
     $get_comments = $this->db->get('comment_tbl'); 
     // You create separate array for each blog 
     $cmnts['blg_id'] = $get_comments->result_array(); 
     }// Now you have array of comments 
     return $cmnts;   
    }// end of get_comments 

當你需要註釋以博客連接你這樣做:

<ul class="cmmnt"> 
    <?php foreach($comnts as $value) 
    { 
    if($value['blg_id'] == $blg['blg_id']): 
    ?> 
<li> 
    <div class=cmnt_container> 
    <div class=commnt_txt> 
    <span class="h5"> 
     <?php echo $value['comment_txt'] ;?> 
    </span> 
    </div> 
    <?php endif; ?> 
</div> 
</ul> 

或者你可以試試這個(我不知道你是如何顯示您的博客):

<ul class="cmmnt"> 
    <?php foreach($comnts[$blg_id] as $value){?> 
    <li> 
     <div class=cmnt_container> 
     <div class=commnt_txt> 
     <span class="h5"><?php echo $value['comment_txt'] ;?> 
     </span> 
     </div> 
    </div> 
</ul> 

$ BLG [ 'blg_id']是你的博客的ID。

0

這可能是有益的

public function get_comments($blog_ids) 
{ 
    foreach($blog_ids as $row) 
    { 

     $blg_post_id = $row['blg_id']; 
     $this->db->where('post_id', $blg_post_id); 
     $get_comments = $this->db->get('comment_tbl'); 
     $cmnts[] = $get_comments->result_array();//append the comments 

    } 
    return $cmnts;   
} 
// end of get_comments 

請更改您的視圖類似下面

<ul class="cmmnt"> 
<?php 
    foreach($comnts as $value) 
    { 
    foreach($value as $value1) 
    { ?> 
    <li> 
    <div class=cmnt_container> 
    <div class=commnt_txt> 
    <span class="h5"><?php echo $value1['comment_txt'] ;?></span> 
    </div> 
    </div> 
    </li> 
    <?php 
     } 
    } 
?> 
+0

非常感謝。在這方面,我想問你一些我正在使用一點不同的方法。 $ param = $ posts ['cnv_post_id']; // $ posts ['cnv_post_id']保存當前帖子編號 $ comments = $ this-> post_model-> get_comments($ param); 在模型 公共函數get_comments($ cnv_post_id) \t \t { \t \t \t $ \t \t \t $ get_comments = $這個 - > DB->查詢( 'SELECT * FROM cnv_comment其中blog_tbl ='。cnv_post_id「。 「); \t \t \t if($ get_comments-> num_rows> 0)return $ get_comments-> result_array(); return array(); \t \t \t } \t但它沒有給出結果。但如果我明確給$ param ='100'它返回結果。 – user3177114

+0

對不起,我沒有得到你想要的東西 – Sundar

+0

http://stackoverflow.com/questions/21311037/database-record-fetch-issue-in-codeigniter 請訪問此鏈接 – user3177114