【发布时间】:2015-05-16 13:33:41
【问题描述】:
我有一个几乎可以完美运行的购物车。但是,我希望能够删除产品而不必将数量设置为 0。因此购物车中的每个产品都有自己的删除按钮,名称 ='delete'。
现在我在页面顶部有这段代码。我已经有一个“更新购物车”按钮,它是代码的上半部分。代码的下半部分是删除按钮的代码。
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
if(isset($_POST['submit'])){
foreach($_POST['quantity'] as $key => $val) {
if($val==0) {
unset($_SESSION['cart'][$key]);
}else{
$_SESSION['cart'][$key]['quantity']=$val;
}
}
}
if(isset($_POST['delete'])){
foreach($_POST['quantity'] as $key => $val) {
unset($_SESSION['cart'][$key]);
}
}
?>
<h2>Your shopping cart</h2>
<p class="lead">Since the beginning, Movingscales™ has become one of the world's biggest scale models sellers with thousands of orders a year. We sell scale models in every kind of transportation, from motorcycles to planes. We don't just sell these products, but we are also involved in the production and design of the models. This is the reason why our products are premium-quality scale models. You can buy the products from our Movingscales™-collection through our online store or in one of our stores around the world.</p>
<form method="post" action="thecollection.php?page=cart">
<table class="table table-striped">
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th>Total price</th>
<th></th>
</tr>
<?php
$sql="SELECT * FROM products WHERE productCode IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.="'".$id."',";
}
$sql=substr($sql, 0, -1).") ORDER BY productName ASC";
$query=mysql_query($sql) or die(mysql_error());
$totalprice=0;
while($row=mysql_fetch_array($query)){
$subtotal=$_SESSION['cart'][$row['productCode']]['quantity']*$row['buyPrice'];
$totalprice+=$subtotal;
?>
<tr class="registerform">
<td><?php echo $row['productName'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['productCode'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['productCode']]['quantity'] ?>" /></td>
<td>€<?php echo $row['buyPrice'] ?></td>
<td>€<?php echo $_SESSION['cart'][$row['productCode']]['quantity']*$row['buyPrice'] ?></td>
<td><button class="btn btn-login" type="submit" name="delete"><i class="fa fa-trash-o"></button></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5"><h4>Total price: <i>€<?php echo $totalprice ?></i></h4></td>
</tr>
</table>
<br />
<a href="thecollection.php?productline=all&scale=all&minimum=10&maximum=110&sortby=Name (A...Z)" class="btn btn-login">Continue shopping</a>
<button class="btn btn-login" type="submit" name="submit">Update cart</button>
<button class="btn btn-login" type="submit" name="submit">Checkout</button>
为什么它不起作用?每当您单击删除按钮时,我基本上都想取消它。
【问题讨论】:
-
html 是什么样子的?
-
@jeroen 我用 cart.php 页面的整个代码编辑了我的帖子。
标签: php sql shopping-cart cart