pass two query result to view in codeigniter

2019-09-15 02:47发布

问题:

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.

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);

Edit:

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>

回答1:

you can do it like This.

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

$resultq2 = array();$i=0;
foreach($resultq1 as $row){
   $id = $row['id'];
   $q2 = $this->db->select("count('id') as count ")->from('sp_topic spft')>where('spft.formid',$id)->get();
   $resultq2[$i]['count'] =  $q1->row_object();
   $i++; 
}
$data['resultq2'] = $resultq2;
$this->load->view('myview', $data,true);

OR

You can use array_merge

like

$data = array_merge($resultq1, $resultq2);

Then in the myview you will get both the results in variables $resultq1, and $resultq2.

You can pass any numbers of the variables from the controller to the view file by $data['variable_name'] and it can be retrieved in the view file like simple variable $variable_name.

Some Sample links which might help :- Passing 2 types of array variables in codeigniter view files



回答2:

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

change this like below:

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