从购物车

时间:2015-05-16 13:33:41

标签: php sql shopping-cart cart

我有一个几乎完美的购物车。但是,我希望能够删除产品而无需将数量设置为0.因此购物车中的每个产品都有自己的删除按钮,名称='删除'。

现在我在页面顶部有这个代码。我已经有了一个更新购物车'按钮,这是代码的上半部分。代码的下半部分是删除按钮的代码。



<?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>
&#13;
&#13;
&#13;

为什么它不起作用?我想在你点击删除按钮的时候基本取消它。

1 个答案:

答案 0 :(得分:0)

如果每个产品都有自己的删除按钮,您如何判断单击了哪个按钮?

要删除购物车中的商品,您需要知道选择了哪种商品。

您可以使用以下链接替换删除按钮:

<a href="/store/cart.php?act=delete&productid=$productid">delete</a>

在表单中添加隐藏值:

<input type=hidden name=product_to_delete value=0>  
...
<input type=button name=deletebtn   onclick="javascript:document.cart_form.product_to_delete.value=$productid; document.cart_form.submit()"; >

或者

您可以为每个产品添加一个复选框,然后选择一个&#34;删除&#34;按钮为整个列表,当您单击“删除”按钮时,您的php应删除每个选定的产品ID。