Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.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
在购物车中显示总计-Cakephp 3_Php_Shopping Cart_Cakephp 3.x - Fatal编程技术网

在购物车中显示总计-Cakephp 3

在购物车中显示总计-Cakephp 3,php,shopping-cart,cakephp-3.x,Php,Shopping Cart,Cakephp 3.x,朋友们,如果有人能分析,我将不胜感激 我无法在购物车中显示总价 我的index.ctp如下所示: <?php foreach((array) $this->request->getSession()->read('cart') as $index=>$cart): ?> <?= $cart->has('product') ? $this->Html->link($cart->product->name, ['contro

朋友们,如果有人能分析,我将不胜感激

我无法在购物车中显示总价

我的index.ctp如下所示:


<?php foreach((array) $this->request->getSession()->read('cart') as $index=>$cart): ?>

<?= $cart->has('product') ? $this->Html->link($cart->product->name, ['controller' => 'Products', 'action' => '/', $cart->product->id]) : '' ?>

<?php echo number_format($cart->product->price, 2, ',', '') ?>


Quantity
<?php echo $this->Form->create('Orders',array('id'=>'add-form','url'=>array('controller'=>'Orders','action'=>'update', $index)));?> 

<?php echo $this->Form->control('quantity', array('type'=>'number', 'label'=>false,'min'=> 1,'size' => 2, 'maxlenght' => 2,'class'=>'form-control form-control-sm mt-1','required' => 'true', 'value'=>$cart->quantity));?>


Subtotal
<?php $sub = ($cart->product->price * $cart->quantity);
echo number_format($sub, 2, ',', '');
?>

<?php endforeach; ?>
</div>

Total: <?php
        $sum = 0;
        foreach(array($sub) as $total){ $sum+= $total; }
        echo $sum;
        ?>

量
小计
总数:
我不知道是否最好的方法是在index.ctp中,但在这里它显示最后一个元素的小计。它不会将所有产品的小计价值相加

当我这样做时:

 <?php
                  $sum = 0;
                  foreach(array($sub) as $total)
                  {
                    debug($sub);
                   $sum+= $total;
                 }
                 echo $sum;
                 ?>

返回:

(第127行) (浮动)35


这是列表中最后一个元素的小计值。我需要得到小计的所有元素才能添加。我感谢您的评论

在第一个
foreach
循环之前,需要将
$sum
初始化为0,然后将新的
foreach
循环放入另一个循环中。总产量的最终结果是在正确的位置上。这正是它。谢谢您您需要在第一个
foreach
循环之前将
$sum
初始化为0,然后将新的
foreach
循环放入另一个循环中。总产量的最终结果是在正确的位置上。这正是它。非常感谢。