2013-03-22 113 views
5

我只使用控制器和视图。
使用以下代码我显示帖子和帖子有回复。
我用下面的控制器和查看来显示帖子和回复。如何将递归函数的结果传递到查看

通过使用查询1我从帖子表中选择帖子作为主题ID(例如,主题ID = 34)并使用此获得帖子ID 30和31。

通过使用查询2我从回复表中选择每个帖子ID的回复。
说具有响应ID 1和
交ID 31交ID 30通过使用第二for循环我正在选择具有响应ID 2.

如果任何答复使用PARENT_ID柱从回复表具有哪吒答复。(使用递归调用以showreply()函数)

我卡在我如何通过递归函数showreply()在视图中的结果。

通过使用我的代码DISPLY:

后30
---为岗位30
后31
第一个回复---为岗位31

答复,但我想告诉喜欢。

后30
---为岗位30
第一个回复---回复到第一个回复
发布31
---为岗位31
第一个回复---回答第一个答复

我已经使用递归函数调用来获得答复使用父母的ID回复,但我没有得到如何通过这个查看。

控制器:答复表

<?php 
public function viewpost() 
    { 
     //Query 1 

     $topicid = trim($this->input->get('topicid')); 
     $q =$this->db->select(array(
      'fp.id as id', 
      'fp.postdata', 
      'fp.topicid')) 
      ->from('forum_post AS fp') 
      ->where('fp.topicid',$topicid) 
      ->order_by('fp.id DESC')->limit(0,10)->get(); 

     $resultq1 = $q->result_array(); 
     $data['resultq1'] = $resultq1; 

     //$data['resultq1'] = $res; 
     $resultq2 = array(); 
     foreach ($resultq1 as $rec) 
     { 
      //Query 2 

      $postid = $rec['id']; 
      $q1 =$this->db->select(array(
       'fr.id as id', 
       'fr.reply_data')) 
       ->from('forum_reply AS fr') 
       ->where('fr.postid',$postid) 
       ->order_by('fr.id ')->get(); 

      $resultq2[$postid] = $q1->result_array(); 
      $data["resultq2"][$postid] = $resultq2[$postid]; 

      foreach($q1->result_array() as $row1) 
      { 

       //second for loop 

        $reply_id = $row1['id']; 
        $resultq3[$reply_id] = $this->showreply($reply_id); // call to function 
        $data["resultq3"] = $resultq3[$reply_id]; 


      }//inner for loop 
     } //outer for loop 

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

    public function showreply($reply_id) 
    { 

        $reply_id1 = $reply_id; 
        $q1 =$this->db->select(array(
         'fr.id as id', 
         'fr.reply_data', 
         'fr.parent_id')) 
         ->from('forum_reply AS fr') 
         ->where('fr.parent_id',$reply_id1) 
         ->order_by('fr.id ')->get(); 

         //print $this->db->last_query(); 
         $resultq4[$reply_id1] = $q1->result_array(); 
         $data["resultq4"]= $resultq4[$reply_id1]; 

         $i=0; 
         foreach($q1->result_array() as $row4) 
         { 
           print_r($q1->result_array()); 
           echo "id".$id = $row4['id']; 
           $parent_id = $row4['parent_id']; 
           if($parent_id!=0) 
           { 
            //$data['nested'][$i] = $q1->result_array(); 
            $this->showreply($id); //recursive call to function       
            $i++; 
           } 
         } 

      return $resultq4; 
    } 
?> 

表结构:

Rep_id rep_text      post_id  Parent_ID 
------------------------------------------------------------------------- 
    1   Reply for post 30    30   null 
    2   Reply for post 31    31   null 
    3   reply to Rep_id 1    null   1 
    4   Rep_id 3 have Rep_id 4   null   3 
    5   Reply for post 31    null   2 
    6   Reply for Rep_id 5    null   5 

---------------------------------------------------------------------------- 

邮政表:

post_id topic id post_title post_desc 
----------------------------------------- 
    30  34   xyz   sssss 
    31  34   pqr   tyyyu 

---------------------------------------- 

* 查看: *

<div> 
    <?php foreach($resultq1 as $row) 
    { ?> 
     <ul> 
      <li></li>  // used to display post 
     </ul> 
    <?php foreach($resultq2 as $rows) 
    { 
     foreach($rows as $row1) 
     { 
      if($row['id']==$row1['postid']) 
      { ?> 
      <ul> 
       <li></li>  // used to display reyly for post 
      </ul> 
      <?php foreach($resultq3 as $rows) 
      { 
       foreach($rows as $row2) 
       { 
        if($row1['id']==$row2['parent_id']) 
       { ?> 
        <ul> 
         <li></li> // used to display reply for reply 
        </ul> 
       <?php  
       }//if 
       } //inner for $row2 
      } // outr for $resultq3 
      } //if 
     } //inner for of $row1 
     }//outer for $resultq2 
    } ?> 
    </div> 
+0

你能更清楚吗? – 2013-03-23 04:44:50

+0

只会有4个回复..? – 2013-03-23 05:00:12

+0

查看我的编辑问题 – Kango 2013-03-27 05:55:46

回答

2

我很抱歉,但是这是一个非常糟糕的代码:(。我会建议重写整个事情..

例如

表的帖子:

ID type title content parent_id post_id 
1 post  xyx  asd  Null  1 
2 comment asd  DEMO  1   1 
2 comment com  DEMO2  2   1 

现在这是我的建议,

所有职位应在1台

与列类型(枚举场[post,comment])

a parent_id列将指定它在树中的位置。

和将所有这些粘合在一起的post_id表。

现在所有你需要做的就是

$post = $this->db->select('*')->where('post_id',$post_id)->get('posts'); 

现在$职位将包括其意见和subcomments,一切所有的帖子..

在一个单一的数据库查询

,所有你需要做的现在是时候组织他们在assoc命令阵列

$post = $this->db->select('*')->where('post_id',$post_id)->get('posts'); 

if($post->num_rows==0)return false;//end if there is no post found 

//step 1 find main post 
$main_post=FALSE; 
foreach($post as $fp){ 
if($fp->type=='post')$main_post=array ('id'=>$fp->id,'title'=>$fp->title,'content'=>$fp->content); 
} 

if($main_post==false)return false;///THERE WAS NO MAIN POST FOUND !!! 

//step 2 get comments on post 

$comments=array(); 

foreach($post as $fp){ 
if($fp->type=='comment' && $fp->parent_id==$main_post['id']){ 
$comment[$fp->id]=array('title'=>$fp->title,'content'=>$fp->content,'child'=>array()); 
} 
} 

//step 3 comments of a comment 

//IF comment is not on parent so its on another comment right :) ! 
foreach($post as $fp){ 
if($fp->type=='comment' && $fp->parent_id != $main_post['id']){ 
$comment[$fp->parent_id]['child'][]=array('title'=>$fp->title,'content'=>$fp->content); 
} 
} 

现在你有inisde $评论变种,很容易环路和主后$迈里面所有评论您的意见和建议n_post :)简单视图将

<h2><?=$main_post['title']?></h2> 
<div class='fp_post'> 
<?=$main_post['post']?> 
</div> 
<ul class='comments'> 

<? 
foreach($comment as $c){ 
echo "<li> $c['title'] : $c['content']"; 

if(!empty($c['comment'])){ 
//there is a comments on this comment :) 
echo "<ul>"; 
foreach($c['comment'] as $coc){echo "<li> $coc['title']: $coc['content']</li>";} 
echo "</ul>";//end of comment of comments 
} 
echo "</li>"//end of a comment and its comments 

} 

?> 
</ul> 

我希望这个简单的代码为您排忧解难解决您的问题,这是做的简单而有效的方法,而且我确定你可以使用一些调试-ING try_catch代替错误返回错误,也可以添加一些验证,但这不是我们现在的情况。

希望这个答案对你:)不够好

cheerz

+0

显示的代码非常难看,但假设只允许2级注释,这个想法本身就很好。 – ahmad 2013-03-31 22:18:52

+0

嗨,即时兴趣,你究竟对代码有什么憎恶? – Zalaboza 2013-04-01 10:16:14

2

这就是我的回答:

首先

考虑使用MVC的形式给出,在模型中分离你的数据库查询部分应用程序。

你应该总是请检查是否存在导致您的查询或没有。这可以防止您的代码在您的查询中没有结果时发生致命错误,并尝试对其进行foreach。:

<?php 
public function viewpost() 
{ 
    //Query 1 

    $topicid = trim($this->input->get('topicid')); 
    $q =$this->db->select(array(
      'fp.id as id', 
      'fp.postdata', 
      'fp.topicid')) 
      ->from('forum_post AS fp') 
      ->where('fp.topicid',$topicid) 
      ->order_by('fp.id DESC')->limit(0,10)->get(); 
    //$data['resultq1'] = $res; 
    $resultq2 = array(); 
    if($q->num_rows() > 0) 
    { 
      $resultq1 = $q->result_array(); 
      foreach ($resultq1 as $rec) 
      { 
       //Query 2 

       $postid = $rec['id']; 
       $q1 =$this->db->select(array(
        'fr.id as id', 
        'fr.reply_data')) 
        ->from('forum_reply AS fr') 
        ->where('fr.postid',$postid) 
        ->order_by('fr.id ')->get(); 

      // the rest of your code 

    } 

    // the rest of your code 

我以为你会显示你的相同第一级的答复为你的第二个层次的答复(而不是嵌套):

- 回复发帖

- reply to reply

- 回复后

- 回复回复

<div> 
<?php 
if($resultq1) 
{ 
    echo '<ul>'; 
    foreach($resultq1 as $row) 
    { 
     echo '<li>';  
     echo $row;// used to display post 
     if(resultq2) 
     { 
     echo '<ul>'; 
     foreach($resultq2 as $rows) 
     { 
      echo '<li>'; 
      echo $rows; // display reply to post 
      echo '</li>'; 
      if($resultq3) 
      { 
       foreach($resultq3 as $row1) 
       { 
        echo '<li>'; 
        echo $row1; // display reply to reply 
        echo '</li>'; 
       } 
      } 
      } 
      echo '</ul>'; 
     } 
     echo '</li>'; 
    } 
    echo '</ul>'; 
    } 
    </div> 

到目前为止,这是我的回答,我希望这帮助你。

+0

您指出的解决方案并不涵盖所有可能性。在这种情况下,递归解决方案是最好的方法,因为有N个级别的答复。我试图找出这样的问题的解决方案。当我到达它时,我会在这里发布解决方案的链接。 – Elkas 2015-08-08 13:52:48