如何在codeigniter中查看查询结果
我使用了两个查询来获得视图的结果。 要获得resultq1的结果,我使用foreach,但如何才能在视图中获得resultq2的结果 对于resultq1的每一行,我在resultq2中得到记录“reccount”如何在codeigniter中查看查询结果,codeigniter,Codeigniter,我使用了两个查询来获得视图的结果。 要获得resultq1的结果,我使用foreach,但如何才能在视图中获得resultq2的结果 对于resultq1的每一行,我在resultq2中得到记录“reccount” Array ( [0] => stdClass Object ( [reccount] => 0 ) [1] => stdClass Object ( [reccount] => 0 ) [2] => stdClass Object ( [reccoun
Array ( [0] => stdClass Object ( [reccount] => 0 ) [1] => stdClass Object ( [reccount] => 0 ) [2] => stdClass Object ( [reccount] => 0 ) [3] => stdClass Object ( [reccount] => 2 ) ) 0002
//控制器
//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
传递给视图
在控制器中,在调用视图之前
$data['resultq2'] = $resultq2
现在,您可以在视图中使用$resultq2
PS-SQL查询应该在模型中,而不是在控制器中。加载视图之前只需写入“$data['resultq2']=$resultq2”
$data['resultq2'] = $resultq2
$this->load->view('forumview', $data, true);
您可以通过$result2变量在视图中检索它
只需执行var\u转储($result2)
在视图文件中,您将获得$result2 varaible的完整数据
xbonez是对的,一定要在模型中编写DB查询,controller只用于登录,以及在(模型、视图、库、助手等)之间进行协调。我对第二个查询进行了一些优化,但逻辑基本相同
$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>
细节
答复
开始于
创建于
//在这里,我想使用resultq2
这应该可以解决问题。
修改控制器-另请参见注释
Xbonez:我也使用这个,但问题是如何在我的视图中获得它。请查看我的视图。您可以为我做一件事:在运行此命令时给我返回的内容:$resultq1=$q->result_array();打印($resultq1)
因为我没有数据库,我可以严格测试数组([0]=>Array([id]=>4[name]=>newforum[added\u by]=>16[added\u on]=>2012-08-04 21:55:12)[1]=>Array([id]=>3[name]=>Third forum[added\u by]=>innovation[added\u on]=>2012-08-04 21:55:12][2]=>Array[id]=>2[name]=>secondary forum[added\u]=>robert[added_on]=>2013-02-18 02:44:38][3]=>Array([id]=>1[name]=>First Forum[added_by]=>joyson[added_on]=>2013-02-18 02:44:38))$resultq1=$q->result_Array();打印($resultq1)
。你给我的是第二个问题。我需要第一个查询。这就是我认为你现在的问题所在抱歉,你能做一个var_导出,这样我就可以使用这些值了。那会让我更快。thanksarray(0=>数组('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'=>'Second Forum','added_by'=>'robert','added_on'=>'2013-02-18 02:44:38',3=>数组('id'=>'1','name'=>'First Forum','added_by'=>'joyson','added_on'=>'2013-02-18 02:44:38',),)
<?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>