使用codeigniter返回递归执行查询的结果

时间:2013-04-01 09:01:09

标签: codeigniter

我是codegniter的新手。 我使用以下代码递归执行函数。

在第一个调用中,我传递了reply_id,然后在函数中我使用了查询来选择id为 parent_id = reply_id然后在foreach中我为所有选定的id执行相同的过程。

现在我想为所有递归返回combine数组。因为每次执行id查询并且每次都获得新结果。
我怎么能这样做?

$resultq3 = $this->showreply($reply_id); //first call to function

<?php
    public function showreply( $reply_id ) {
       $q1 =$this->db->select('*')
                 ->from('forum_reply AS fr')
                 ->where('fr.parent_id',$reply_id)
                 ->order_by('fr.id ')->get();;

       foreach( $q1->result_array() as $row4 ) {
          $id = $row4['id'];  
          $parent_id = $row4['parent_id'];
          if( $parent_id !=0 ) {  
              $this->showreply( $id );
          }
       }
       return  $result; //here want to return result
    }
 ?>

编辑代码:

$resultq3 = $this->showreply($reply_id); //first call to function

    <?php
        public function showreply( $reply_id ) {
           $q1 =$this->db->select('*')
                     ->from('forum_reply AS fr')
                     ->where('fr.parent_id',$reply_id)
                     ->order_by('fr.id ')->get();;

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

              $arr1 = $q1->result_array();
              $arr = array_merge($arr, $arr1);

              $id = $row4['id'];  
              $parent_id = $row4['parent_id'];
              if(!empty($arr1)) {  
                  $this->showreply( $id );
              }
           }
           return $arr;
        }
     ?>

1 个答案:

答案 0 :(得分:0)

public function showreply($parent_id = 0)
{
    $q1 = $this->db->select('*')
                    ->from('forum_reply AS fr')
                    ->where('fr.parent_id', $parent_id)
                    ->order_by('fr.id ')->get();

    $arr = array();
    foreach ($q1->result_array() as $row4)
    {
        $arr1 = $row4;

        $id = $row4['id'];
        $parent_id = $row4['parent_id'];
        $arr1['child'] = $this->showreply($id);
        $arr[]=$arr1;
    }

    return $arr; //here want to return result
}