传递两个查询结果以在codeigniter中查看 [英] pass two query result to view in codeigniter

查看:50
本文介绍了传递两个查询结果以在codeigniter中查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个查询$ q1和$ q2.从query1中,我获得了多个记录,每个记录都有id,我想在条件中使用此id进行第二次查询.

i have two query $q1 and $q2. from the query1 i get multiple records and each record having id and i want use this id for second query in where condition.

我正在控制器中执行此操作.我试过下面的代码.在foreach中,我试图存储id并传递到$ q2条件.

i am doing this in controller. i have tried following code. In the foreach i am trying to store id and pass to $q2 where condition.

//Query 1
    $q1 = $this->db->select(array(
                                 'spf.id as id' ,
                                  'spf.name',
                                'spf.added_on'
                                ))->from('sp_form spf')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
                   $data['data'] = $q1->result_array();

                foreach($data as $rec)
                {
                   $id = $rec['id']; // here how i catch id for each row

                 //Query 2

                    $q2 = $this->db->select("count('id') as count ")->from('sp_topic spft')->where('spft.formid',$id)->get();
                      $data['count'] =  $q1->row_object();
                }
                // pass combine result to view

                 $this->load->view('myview', $data,true);

This is my view.

I have try Nishant answer and i get resultq1 using foreach but how can i get result of resultq2.


    <table width="100%">
      <tr>
        <td class="th"> Details</td>
        <td width="5%" class="th"> Answer</td>
        <td width="15%" class="th">Started by</td>
        <td  width="15%" class="th">Created on</td>
      </tr>
      <?php foreach($resultq1 as $row):?>
       <tr>
        <td><?php echo $row['name'] ;?></td>

         <td >---- </td> // here i want to use resultq2

         <td><?php echo $row['added_by'] ;?></td>
        <td ><?php echo $row['added_on'];?></td>
      </tr>
      <?php endforeach;?>
    </table>

推荐答案

$data['count'] =  $q1->row_object();

如下更改:

foreach($data as $rec)
       {
          $id = $rec['id']; // here how i catch id for each row
          $q2 = $this->db->select("count('id') as count ")->
          from('sp_topic spft')->where('spft.formid',$id)->get();
          $data[$id]['count'] =  $q2->result_array();
       }

$this->load->view('myview', $data,true);

这篇关于传递两个查询结果以在codeigniter中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆