【问题标题】:how to get result of query in view in codeigniter如何在codeigniter中查看查询结果
【发布时间】:2013-03-01 04:22:08
【问题描述】:

我使用两个查询来获取视图上的结果。 要获得 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

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

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

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

    $data['resultq2'] = $resultq2
    

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

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

    【讨论】:

    • Xbonez:我也用这个,但问题是我怎样才能在我的视图中得到这个。看我的视图。
    【解决方案2】:

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

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

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

    只要在你的视图文件中做var_dump($result2);,你就会得到$result2变量的完整数据。

    xbonez 是对的,一定要在模型中编写数据库查询,控制器仅用于登录,以及(模型、视图、库、帮助程序等)之间的协调

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        这应该可以解决它。

        修改控制器 - 另见 cmets

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

        我没有时间测试它,但它应该可以工作。如果您发现任何问题,请告诉我:

        【讨论】:

        • 你能为我做一件事吗:给我返回的是你运行这个:$resultq1= $q-&gt;result_array(); print_r($resultq1)因为我没有db,我可以严格测试
        • Array ( [0] => Array ( [id] => 4 [name] => 新论坛 [add_by] => 16 [add_on] => 2012-08-04 21:55: 12 ) [1] => Array ( [id] => 3 [name] => 第三次论坛 [add_by] => 创新 [add_on] => 2012-08-04 21:55:12 ) [2] => Array ( [id] => 2 [name] => 第二个论坛 [add_by] => robert [add_on] => 2013-02-18 02:44:38 ) [3] => 数组 ( [id] => 1 [ name] => 第一个论坛 [add_by] => joyson [add_on] => 2013-02-18 02:44:38 ) )
        • $resultq1= $q-&gt;result_array(); print_r($resultq1)。你给我的是第二个查询。我需要第一个查询。这就是我认为你现在的问题所在
        • 对不起,你能不能做一个 var_export 以便我可以使用这些值。那会让我更快。谢谢
        • array ( 0 => array ( 'id' => '4', 'name' => '新论坛', ' added_by' => 'jack', ' added_on' => '2012 -08-04 21:55:12', ), 1 => 数组 ('id' => '3', 'name' => '第三论坛', ' added_by' => '创新', ' added_on' = > '2012-08-04 21:55:12', ), 2 => 数组 ('id' => '2', 'name' => '第二论坛', ' added_by' => 'robert', ' added_on' => '2013-02-18 02:44:38', ), 3 => 数组 ('id' => '1', 'name' => 'First Forum', ' added_by' => 'joyson ', 'add_on' => '2013-02-18 02:44:38', ), )
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-27
        相关资源
        最近更新 更多