2014-01-23 158 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