1
我需要显示一个名为cart的表中的所有产品。但是,无论桌面上有多少产品,json_decode
总是会在用户端返回1个产品的数组(请查看var_dump)。用户只能看到最后添加的产品。 json只显示表格的最后一行。我需要它来显示所有产品。我已经尝试了很多东西来调试这个应用程序,但我认为我不完全理解JSON的功能。json_decode返回数组1与mysqli
的var_dump($项目):array(1) { [0]=> array(4) { ["id"]=> string(2) "14" ["size"]=> string(1) "0" ["quantity"]=> string(1) "1" ["available"]=> string(1) "1" } }
cart.php
<?php
if($cart_id !=''){
$cartQ = $db->query("SELECT * FROM cart WHERE id ='{$cart_id}' ");
$result = mysqli_fetch_assoc($cartQ);
$items = json_decode($result['items'],true);
$i = 1;
$sub_total = 0;
$item_count = 0;
}
?>
<?php
foreach($items as $item){
var_dump($items);
$product_id = $item['id'];
$productQuery = $db->query("SELECT * FROM product WHERE id ='{$product_id}' ");
$product = mysqli_fetch_assoc($productQuery);
?>
<tr class="p">
<td class="image"><img src="<?=$product['image_1'];?>" /></td>
<td class="name"><?=$product['prod_name'];?></td>
<td class="price"><?=money($product['price']);?></td>
<td class="quantity"> <?=$item['quantity'];?></td>
<td class="pricesubtotal"><?=money($item['quantity'] * $product['price']);?></td>
<td class=""><div><button name='removeitem' onclick="update_cart('removeitem','<?=$product_id['id'];?>');">×</button></div></td>
</tr>
<?php endif;?>
感谢您的输入......我明白你在说什么,并且我明白错误是从哪里来的。基本上json在这种情况下是无用的......我应该为id和其他所有内容创建单独的列。 – Waterfalls
是的,这几乎总结了它。旧的答案中有很多标准的车台设计 – e4c5