$data = array();
$session_id = $this->session->userdata('id');
$q = $this->db->get_where('cust_orders', array('cust_id' => $id));
foreach ($q->result() as $row)
{
$data['id'] = $row->cust_id;
$data['orders'] = $row->cart;
}
return $data;
将data
传递给视图
<?php foreach($_orders as $key => $item) : ?>
<tr>
<td>
<?php
$price = $item['price']; #change this to order status
switch ($price)
{
case (64); $UPSTrack = '1Z12345E1512345676'; #Shipped add tracking number
echo 'Shipped <br> <a href="http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums='.$UPSTrack.'">Track My Order</a>';
break;
case (88);
echo 'Shipped';
break;
default;
echo 'Processing';
break;
}
?>
</td>
<td>
<?php foreach ($item['options'] as $option => $value)
{
echo '<span class="option">'.$option . ': </span> <span class="options">' . $value . '</span>';
}
?>
</td>
<td><?php echo $item['id']; ?></td>
<td><?php echo $item['qty']; ?></td>
<td><?php echo number_format($item['price'],2); ?></td>
</tr>
<?php endforeach; ?>
选项是ARRAY
包含foreach
,其中包含变量array
$data['orders'] = $row->cart;
在数据库中,我按cust_orders
查找cust_id
表。客户可能有超过1
行,表示多个订单。
问题是我只是在最后一行循环。我需要遍历cust_id
=到$session_id
的所有行。
我觉得问题出在我的MODEL
,因为如果我将$data['id'] = $row->cust_id;
更改为echo $row->cust_id;' I am able to see all of the rows which match the customers id, but it breaks my
foreach`代码。
如何正确分配values
查询中返回的MODEL
,以便显示所有cust_orders ==到$session_id
?
答案 0 :(得分:1)
现在,您将使用每个客户值覆盖您的数组,因此您需要多维数组。
应该如下所示
编辑:在模型中更改您的foreach,如下所示
foreach ($q->result() as $row)
{
$data['id'] = $row->cust_id;
$data['orders'][] = $row->cart;
}