2

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

4 回答 4

4

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

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

$data['resultq2'] = $resultq2

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

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

于 2013-03-01T05:00:34.457 回答
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>
于 2013-03-01T05:18:47.460 回答
0

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

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

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

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

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

于 2013-03-01T05:09:19.240 回答
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>

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

于 2013-03-01T05:26:39.903 回答