Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中填充购物车的项目计数_Php - Fatal编程技术网

在php中填充购物车的项目计数

在php中填充购物车的项目计数,php,Php,我有一个购物车页面,我想在标签的括号内显示物品数量 问题是它总是显示为零,因为它位于代码上方。此外,由于PHP过程代码自上而下,很难对代码进行重新排序 我找不到办法。我怎么能这么做 // some codes if($cart_id != ''){ $cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'"); $result = mysqli_fetch_assoc($cartQ); $items

我有一个购物车页面,我想在
标签的括号内显示物品数量

问题是它总是显示为零,因为它位于代码上方。此外,由于PHP过程代码自上而下,很难对代码进行重新排序

我找不到办法。我怎么能这么做

// some codes

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;
 }
 ?>

<h2 style="color:#00484F;">Cart(<?=$item_count?>)</h2>

// some codes

<?php
             foreach($items as $item){
               $product_id = $item['id'];
               $productQ = $db->query("SELECT * FROM products WHERE id = '{$product_id}'");
               $product = mysqli_fetch_assoc($productQ);
               $sArray = explode(',',$product['ages']);
               foreach($sArray as $ageString){
                 $s = explode(':',$ageString);
                 if($s[0] == $item['age']){
                   $available = $s[1];
                 }
               }
               ?>

// some codes

<button class="btn btn-sm btn-light" id="button1" onclick="update_cart('removeone','<?=$product['id'];?>','<?=$item['age'];?>');">-</button>
                     </span>
                  <span class="boxed6">
                   <?=$item['quantity'];?>
                 </span>
                   <?php if($item['quantity'] < $available): ?>
                     <span class="boxed7">
                     <button class="btn btn-sm btn-light" id="button2" onclick="update_cart('addone','<?=$product['id'];?>','<?=$item['age'];?>');">+</button>
                     </span>
                   <?php else: ?>
                     <span class="text-danger">Max</span>
                   <?php endif; ?>
                 </div>
                   <p class="text-right" style="margin-right:50px; color:#1C5D6B;"><?=money($item['quantity'] * $product['price']);?></p>
                   <hr>
              <?php
                $i++;
                $item_count += $item['quantity'];
                $sub_total += ($product['price'] * $item['quantity']);
              }
              ?>
//一些代码
如果($cart\u id!=''){
$cartQ=$db->query(“从购物车中选择*,其中id='{$cart\u id}'”;
$result=mysqli\u fetch\u assoc($cartQ);
$items=json_decode($result['items'],true);
$i=1;
$sub_total=0;
$item_count=0;
}
?>
购物车()
//一些代码
//一些代码

您可以通过使用提取数量值并对其求和来分别求和,从而提前获得项目计数。根据您的代码,这应该可以工作:

$item_count = array_sum(array_column($items, 'quantity'));

在您当前拥有
$item\u count=0的位置使用此选项

@MyDB很高兴我能帮上忙。如果这确实解决了你的问题,请考虑标记接受的答案(在上/下投票箭头下的复选标记)。请看,我发现了一个与$item_count=array_sum(array_column($items,'quantity'))无关的问题;。如果我删除了数据库中的购物车,我会得到一个错误:第125行为foreach()提供的参数无效。只有从浏览器中删除cookie后,此问题才会得到解决。。。如何解决这个问题???@MyDB你应该问一个新问题。它需要太多的信息才能放在评论中