我在模型中有查询,从表1中选择包含数据的数据:
Column NOPE
数据1
数据2
数据3
然后我想从table1的结果中选择所有数据,但只得到第一行:
不 |总
否1 | 20人
如何循环所有行以选择并显示所有行在我的视图中? 所以我像这样从选择中获取所有行:没有总计
否1 20人
否2 30人
否3 25人
否4 40人
这是我的模型:
function test()
{
$query = $this->db->query("select nope from tb1");
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$nopen=$row['nope'];
$query2=$this->dbmssql->query("
select nope,
count(name) as total
from trans
where nope ='$nopen'
and date='2016-04-20'
group by nope ");
return $query2->result_array();
}
}
}
我的观点是:
<tbody>
<?php
$no=0;
foreach ($res as $row) { $no++?>
<tr>
<td><?php echo $no;?></td>
<td><?php echo $row['nope'] ;?></td>
<td><?php echo $row['total'];?> PAX</td>
</tr>
<?php
} ?>
</tbody>
请帮助我,非常感谢。
你只能得到第一行,因为你的foreach循环只执行一次,因为循环在返回语句处退出。您需要将结果数组分配给一个变量,并在循环后返回该变量。
下面是修改后的代码,用于对$query中的每个记录执行 foreach 循环。
function test()
{
$query = $this->db->query("select nope from tb1");
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$nopen=$row['nope'];
$query2=$this->dbmssql->query("
select nope,
count(name) as total
from trans
where nope ='$nopen'
and date='2016-04-20'
group by nope ");
$all_records[]=$query2->result_array();
}
return $all_records;
}
}
百万 谢谢你,萨兰先生。
最后,我遵循您的代码,并使用以下内容更改我的视图:
<?php
$no=0;
foreach ($res as $row)
{
$no++;
?>
<tr>
<td><?php echo $no;?></td>
<td><?php echo $row[0]['nope'] ;?></td>
<td><?php echo $row[0]['total'];?> PAX</td>
</tr>
<?php
} ?>
现在我得到了所有记录的结果,..你是我的英雄...