【问题标题】:how to combine results of query and return that result using codeigniter如何组合查询结果并使用 codeigniter 返回该结果
【发布时间】:2013-03-27 11:19:45
【问题描述】:

我是 codeigniter 的新手。 我使用以下代码递归执行查询。

假设$q查询选择4个id(10,11,20,24)

然后对于每个 id showreply 函数(在 foreach 中)递归调用然后如何返回组合结果。

$resultq3 = $this->showreply($reply_id); 

    <?php
            public function showreply($reply_id)
            {

                   $q1 =$this->db->select('*')
                    ->from('forum_reply AS fr')
                ->where('fr.parent_id',$reply_id1)
                ->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;
            }
         ?>

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    我不太明白你在这里问的是什么。也许显示 showReply 函数会有所帮助,但是您已经有了组合结果,并且正在将其拆分到 foreach 中,那么问题是什么?另外,为什么要将reply_id 分配给reply_id1?那有什么意义呢?只需在查询中使用 $reply_id。

    您还执行了一个毫无意义的 if 语句,因为您可以过滤掉查询本身中不需要的 id(而且您真的会拥有一个 = 0 的 id 吗?)

    事实上,我越看这段代码就越困惑。 $this->showreply($id) 在哪里填充 $id?

    <?php
        public function showreply($reply_id)
        {
    
            $q1 =$this->db->select('*')
                ->from('forum_reply AS fr')
            ->where('fr.parent_id',$reply_id)
            ->where('fr.parent_id !=',0)
            ->order_by('fr.id ')->get();;
    
            //$i=0;
            foreach($q1->result_array() as $row4)
            { 
                $parent_id = $row4['parent_id'];
                $this->showreply($id);      
            }
    //below is the actual answer to your question on how to return the combined results.
            return  $q1->result_array();
        }
     ?>
    

    好的,重新阅读您的问题后,我想我有了更好的理解。如果您将 id 作为数组传递,如下所示:

    $reply_id =  array(10,11,20,24);
    

    然后您可以修改您的查询以使用:

    $this->db->where_in('fr.parent_id', $reply_id);
    

    这会将结果作为一个包含所有 4 个 ID 的组合结果返回。

    【讨论】:

    • :对不起,我错过了在 foreach.now 中为每次执行新查询时写入 id 现在我想在单个数组中返回所有查询的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 2016-08-14
    • 2015-11-20
    • 1970-01-01
    相关资源
    最近更新 更多