如何在codeigniter中获取查看结果

时间:2013-03-01 04:22:08

标签: codeigniter

我使用两个查询来获取视图上的结果。 要获得resultq1的结果,我使用foreach但是如何在视图中获得resultq2的结果。

对于resultq1的每一行,我在resultq2中得到记录“reccount”。

//控制器

     //Query 1
     $q = $this->db->select(array(
                'spf.id as id' ,
                'spf.name',
                "if(u.username != '',u.username,spf.added_by) as added_by ",
                'spf.added_on'
              ))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();

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

            $resultq2 = array();
             $i=0;
            foreach($resultq1 as $rec)
             {
                      //query 2
                 $id = $rec['id'];
                 $q1 = $this->db->select("count('id') as reccount ")->from('sp_forum_topic spft')->where('spft.forumid',$id)->get();
                 $resultq2[$i] =  $q1->row_object();
                 $i++; 
               }

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

//查看文件

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

在视图中为resultq2打印数组。 Resultq1有4行,因此我在resultq2中得到4个值。

Array ( [0] => stdClass Object ( [reccount] => 0 ) [1] => stdClass Object ( [reccount] => 0 ) [2] => stdClass Object ( [reccount] => 0 ) [3] => stdClass Object ( [reccount] => 2 ) ) 0002

4 个答案:

答案 0 :(得分:3)

您还需要将$resultq2传递给视图。

在控制器中,在调用视图之前

$data['resultq2'] = $resultq2

您现在可以在视图中使用$resultq2

PS - SQL查询应该在模型中,而不是控制器。

答案 1 :(得分:0)

在加载视图之前,只需写'$ data ['resultq2'] = $ resultq2',

$data['resultq2'] = $resultq2
$this->load->view('forumview', $data, true);

您可以通过$ result2变量在视图中检索它。

在您的视图文件中执行var_dump($result2);,您将获得$ result2的完整数据。

xbonez是对的,在模型中编写数据库查询,控制器只用于登录,并在(模型,视图,库,帮助器等)之间进行协调。

答案 2 :(得分:0)

我优化了你的第二个查询,但逻辑几乎相同。

$q = $this->db->select(array(
    'spf.id as id' ,
    'spf.name',
    "if(u.username != '',u.username,spf.added_by) as added_by ",
    'spf.added_on'
    ))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();

    $resultq1= $q->result_array();
    $data['resultq1'] = $resultq1;
    $ids = array();
    foreach($resultq1 as $result)
        $ids[] = $result['id'];

     $resultq2 = $this->db->select("count('id') as reccount ")->from('sp_forum_topic spft')->where_in('spft.forumid',$ids)->get();

    foreach( $resultq2->result_array() as $res )
        $newArr[$res['forumid']] = $res;

    $data['resultq2'] = $newArr;

在resultq2被原始id索引之后,它从resultq1循环中易于使用的id来显示正确的数据

<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 >
            <?php

                print_r( $resultq2[$row['id']]);

            ?>
        </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>

答案 3 :(得分:0)

这应该解决它。

修改控制器 - 另请参阅注释

<?php
//Query 1

  $q = $this->db->select(array(
  'spf.id as id',
  'spf.name',
  "if(u.username != '',u.username,spf.added_by) as added_by ",
  'spf.added_on'
  ))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();

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

$resultq2 = array();
foreach ($resultq1 as $rec) {
    //query 2
    //echo $id = $rec['id']; //Test and See if all IDs echo out correctly
    $data["id"] = $id; //Add this for the ID
    $q1 = $this->db->select("count('id') as reccount, spft.forumid")->from('sp_forum_topic spft')->where('spft.forumid', $id)->get();
    $resultq2[$id] = $q1->result(); //Change this line
    $data["resultq2"][$id] = $resultq2[$id]; //Add this line
}

//echo "<pre>";
//$export = var_export($data, true);// Test the returned value...
//echo "</pre>";
$this->load->view('forumview', $data, true);
?>

在您的视图中

<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):  ?>
    <?php foreach ($data as $row): ?>
        <tr>
            <td><?php echo $row['name']; ?></td>
            <td>
                <?php
                //$resultq2 is now array
                //print_r($resultq2); //Do this so you can see the depth of the array

                foreach ($resultq2 as $counts) {
                    foreach ($counts as $count) { //The second foreach might be necessary - please test
                        echo $count->reccount; //This should print out the count result
                        //echo "<br />";
                    }
                }
                ?>
            </td>
            <td><?php echo $row['added_by']; ?></td>
            <td ><?php echo $row['added_on']; ?></td>
            <td >

            </td>
        </tr>
        <?php
    endforeach;
    exit;
    ?>
</table>

我没有时间测试它,但它应该工作。如果您发现任何问题,请与我们联系: