我想根据数量将所有产品价格加在一起。唯一的问题是每个项目都是根据用户选择随机添加的,然后可以将它们调整为任意随机数量。
尝试为存储创建一个额外的数组,将变量添加到一起,但它没有接受QT ammount。
PHP:
<?php
$total = 0;
// Fetch Database
$results->data_seek(0);
foreach ($_SESSION['cart'] as $item => $quantity) {
$row = $results->fetch_assoc();
$pId = $row['ProductID'];
$pName = $row['ProductName'];
$pImg = $row['ProductImg'];
$pPrice = $row['Price'];
$pInfo = $row['About'];
foreach ($quantity as $quantity) {
$priceA = array();
$pushA = $pPrice * $quantity;
array_push($priceA, $pushA);
$bT = implode(",", $priceA);
$total += $priceA;
}
?>
标记
<div class="col">
<img src="img/<?php echo $pImg; ?>">
<h1><?php
echo $pName;
?></h1>
<h2>$<?php
echo $pPrice;
?></h2>
<h3>QT: <?php echo $quantity; ?>
<a href="addc.php?id=<?php echo $pId ?>">+</a>
<a href="rc.php?id=<?php echo $pId ?>">-</a></h3>
</div>
<?php } ?>
<div class="cpan">
<div class="col2">
<div class="allc">
<h1>Your total: <?php echo ($total); ?></h1>
</div>
</div>
</div>
PHP
// Add 1 QT
$item = $_GET['id'];
$quantity = 1;
if(isset($_SESSION['cart'][$item])) {
$_SESSION['cart'][$item] += $quantity;
} else {
$_SESSION['cart'][$item] = $quantity;
}
// Remove 1 QT
$item = $_GET['id'];
$quantity = 1;
if(isset($_SESSION['cart'][$item])) {
$_SESSION['cart'][$item] -= $quantity;
} else {
$_SESSION['cart'][$item] = $quantity;
}
答案 0 :(得分:2)
解决。
<强> PHP:强>
<?php
$total = 0;
// Fetch Database
$results->data_seek(0);
foreach ($_SESSION['cart'] as $item => $quantity) {
$row = $results->fetch_assoc();
$pId = $row['ProductID'];
$pName = $row['ProductName'];
$pImg = $row['ProductImg'];
$pPrice = $row['Price'];
$pInfo = $row['About'];
$priceA = array();
$pushA = $pPrice * $quantity;
array_push($priceA, $pushA);
$bT = implode(",", $priceA);
$total += $bT; // Change to $bT
?>