【发布时间】:2017-09-12 14:23:17
【问题描述】:
我使用 foreach 使用来自“销售”行 json_decode($row['sale']) 的数据制作表格,现在分别显示每个条目,我想要做的是将具有相同 ID [product_id] 的每个条目显示为一个并将 [num ], sum [product_pcs], sum [price_per_pcs] 所有具有相同id的条目并显示一行。
json_decode($row['sale']):
Array ( [0] => stdClass Object ( [stock_id] => 501 [product_id] => 7 [product_pcs] => 1120 [price_per_pcs] => 1.17 [num] => 1) [1] => stdClass Object ( [stock_id] => 500 [product_id] => 7 [product_pcs] => 1120 [price_per_pcs] => 1.17 [num] => 1 ) [2] => stdClass Object ( [stock_id] => 497 [product_id] => 3 [product_pcs] => 600 [price_per_pcs] => 1,1 [num] => 1 ) [3] => stdClass Object ( [stock_id] => 496 [product_id] => 3 [product_pcs] => 600 [price_per_pcs] => 1,1 [num] => 1 ) [4] => stdClass Object ( [stock_id] => 526 [product_id] => 25 [product_pcs] => 1120 [price_per_pcs] => 1.22 [num] => 1 ) [5] => stdClass Object ( [stock_id] => 525 [product_id] => 7 [product_pcs] => 1120 [price_per_pcs] => 1.17 [num] => 1 ) )
我这样显示接收到的数据:
<table>
<thead>
<tr>
<th>No</th>
<th>Product ID</th>
<th>Price Per Piece</th>
<th>Product pcs</th>
<th>Total products</th>
<?php
$count = 1;
$sale = json_decode($row['sale']);
foreach ($sale as $product):
?>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $count++;?>.</td>
<td><?php echo $product->product_id; ?></td>
<td><?php echo $product->price_per_pcs;?></td>
<td><?php echo $product->product_pcs; ?></td>
<td><?php echo $product->num; ?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
通过以上内容,我得到了一个如下所示的表格:
我想看起来像这样:
我将不胜感激。
【问题讨论】:
-
最好的方法是更改 SQL 查询源...你能分享一下吗?
标签: php mysql arrays codeigniter foreach