【问题标题】:Blog and comment issue in CodeigniterCodeigniter 中的博客和评论问题
【发布时间】:2014-01-23 12:46:41
【问题描述】:

目前我遇到了关于博客系统的问题。用户将在哪里发布他们的博客,其他成员可以对其发表评论。当用户打开自己的主页时,他/她可以看到他/她写的博客,每个博客都附有cmets。就像这样-

博客 1 .评论1 .comment2 ...

博客 2 .评论1 .comment2 .Comment3 ....

等等。我有两个关于这个的表

  1. blog_tbl(blg_id,blg_title,blg_content,author_id,crt_date)
  2. comments_tbl(cmmnt_id,blg_id,cmnt_txt,author_id,crt_date)

现在我可以显示所有博客了。但是在显示与特定博客相关的 cmets 时会遇到问题。 要获取 cmets,这些是我的步骤-

  • 我有一个数组$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;          
}
  • 现在我将这个 $cmnts 结果数组数组传递给我的视图。

  • 在此处的视图中,我无法根据帖子区分 cmets

首先$comnts 将每个博客的所有 cmets 混合在一起。如何区分。其次,在 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>

【问题讨论】:

  • 你总是覆盖 cmets 数组而不是附加它

标签: php codeigniter blogs


【解决方案1】:

这可能会有所帮助

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 
       } 
    }
 ?>

【讨论】:

  • 非常感谢 .. 在这种情况下,我想问你一些事情,我正在使用一些不同的方法。 $param = $posts['cnv_post_id']; //$posts['cnv_post_id']保存当前post id $cmets = $this->post_model->get_cmets($param);在模型中 public function get_cmets($cnv_post_id) { $get_cmets= $this->db->query('select * from cnv_comment where blog_tbl='.$cnv_post_id.''); if($get_cmets->num_rows > 0) return $get_cmets->result_array();返回数组(); } 但它没有给出结果。但如果我明确给出 $param = '100' 它会返回结果。
  • 对不起,我没有得到你正在尝试的东西
【解决方案2】:

您将所有 cmets 放在同一个变量 $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。

【讨论】:

    猜你喜欢
    • 2013-04-16
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    相关资源
    最近更新 更多