首先,在这里我们可以看到文件index.php,cart.html.php和catalog.html.php
文件index.php
<?php
$items = array(
array("id" => '1', "name" => 'Meitantei Conan/Case Closed', "category" => "Comics & Manga", "desc" => "nodesc", "price" => '4'),
array("id" => '2', "name" => 'Harry Potter and the Philopher\'s stone', "category" => "Books", "desc" => "nodesc", "price" => '20'),
array("id" => '3', "name" => 'Harry Potter lol', "category" => "Lolling", "desc" => "nodesc", "price" => '20'),
array("id" => '4', "name" => 'My Harry Potter', "category" => "Junk", "desc" => "nodesc", "price" => '20'),);
session_start();
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
if(isset($_POST['action']) && ($_POST['action']) == "Buy") {
$_SESSION['cart'][] = $_POST['buyid'];
header('Location .');
}
if(isset($_POST['action']) && $_POST['action'] == 'Empty Cart') {
unset($_SESSION['cart']);
header('Location: ?cart');
exit();
}
if(isset($_GET['paypal'])) {
echo "I will add here paypal and other things..";
exit();
}
if(isset($_GET['cart'])) {
$cart = array();
$total = 0;
foreach($_SESSION['cart'] as $id) {
foreach($items as $product) {
if($product['id'] == $id) {
$cart[] = $product;
$total += $product['price'];
break;
}
}
}
include 'cart.html.php';
exit();
}
if(isset($_POST['del']) && ($_POST['del']) == 'Delete') {
if(isset($_GET['del'])) {
$cart = array();
$delid = $_POST['delid'];
foreach($_SESSION['cart'] as $id) {
foreach($items as $product) {
if($product['id'] == $delid) {
unset($_SESSION['cart'][$delid]);
}
}
}
}
}
include 'catalog.html.php';
档案catalog.html.php
<?php
if(!isset($_COOKIE['visits'])) {
$_COOKIE['visits'] = 0;
}
$visits = $_COOKIE['visits'] + 1;
setcookie('visits', $visits, time() + 3600 * 24 * 365);
?>
<!DOCTYPE html>
<html lang="it">
<head><title>Products Catalog</title>
<style>
table {
border-collapse: collapse;
}
td, th {
border: 1px solid black;
}
</style>
</head>
<body>
<?php global $visits;
if($visits > 1) {
echo "<h2>Welcome back!! Visit number: $visits</h2>";
}
else echo "<h2>Welcome! This is your first visit!</h2>";
?>
<p>Your cart contains <?php echo count($_SESSION['cart']); ?> items.</p>
<p><a href="?cart">View your cart</a></p>
<table border="1">
<thead>
<tr>
<th>Item Name</th>
<th>Item Desc</th>
<th>Item Category</th>
<th>Price</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<?php foreach($items as $item): ?>
<tr>
<td><?php echo $item['name']; ?></td>
<td><?php echo $item['desc']; ?></td>
<td><?php echo $item['category']; ?></td>
<td>$<?php echo number_format($item['price'], 2); ?></td>
<td><form action="" method="post">
<div>
<input type="hidden" name="buyid" id="buyid" value="<?php echo $item['id']; ?>">
<input type="hidden" name="price" id="price" value="<?php echo number_format($item['price'], 2); ?>">
<input type="submit" name="action" id="action" value="Buy">
</div>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
然后,提交cart.html.php
<?php
if(!isset($_COOKIE['visits'])) {
$_COOKIE['visits'] = 0;
}
$visits = $_COOKIE['visits'] + 1;
setcookie('visits', $visits, time() + 3600 * 24 * 365);
?>
<!DOCTYPE html>
<html lang="it">
<head><title>Cart</title>
<style>
table {
border-collapse: collapse;
}
td, th {
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Your cart</h1>
<?php global $visits;
if($visits > 1) {
echo "<h2>Welcome back!! Visit number: $visits</h2>";
}
else echo "<h2>Welcome! This is your first visit!</h2>";
?>
<?php if(count($cart)>0): ?>
<p>Your cart contains <?php echo count($cart); ?> items.</p>
<p>Total: <?php echo number_format($total, 2); ?></p>
<table border="1">
<thead>
<tr>
<th>Item Name</th>
<th>Item Desc</th>
<th>Item Category</th>
<th>Price</th>
<th>Options</th>
</tr>
</thead>
<tfoot>
<tr>
<td>Tot:</td>
<td><?php echo number_format($total, 2); ?></td>
</tr>
</tfoot>
<tbody>
<?php foreach($cart as $item): ?>
<tr>
<td><?php echo $item['name']; ?></td>
<td><?php echo $item['desc']; ?></td>
<td><?php echo $item['category']; ?></td>
<td>$<?php echo number_format($item['price'], 2); ?></td>
<td><form action="?del" method="post">
<div>
<input type="hidden" name="delid" id="delid" value="<?php echo $item['id']; ?>">
<input type="submit" name="del" id="del" value="Delete">
</div>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p><a href=".">Return back to our catalog!</a></p>
<form action="?" method="post">
<p><a href="?paypal">Continue shopping</a> or <input type="submit" name="action" id="action" value="Empty Cart"></p>
</form>
<?php else: ?>
<p>Your cart is empty! You can buy from our <a href=".">catalog</a></p>
<?php endif; ?>
</body>
</html>
此脚本位于以下网站:http://hydrerscript.altervista.org/
简单来说,我的测试网站,我加载了所有脚本(PHP,JQuery和其他...)
无论如何,问题是什么?
嗯,问题是当我们通过“删除”按钮删除项目时,系统只删除其他项目的重复项目,重复项目(等等...)。
简单示例:
你可以尝试购买和装入你的购物车8件物品,2个柯南,2个我的哈利波特,2个哈利波特和菲利普的石头和2个哈利波特哈哈,然后去查看你的购物车。
这将是图像显示:
Img - &gt; http://dumpshare.net/images/1765764stackoverflow.PNG&lt; - Img
然后,尝试转到购物车,然后点击每个项目的“删除”。
正如您将看到的那样,系统只删除到4个项目,因为正如我所写的那样,只删除了重复的项目,重复项目等等,以及其他项目。
为什么?
如何解决
答案 0 :(得分:0)
问题是你要混合苹果和梨。
将商品添加到购物车时
$_SESSION['cart'][] = $_POST['buyid'];
您将要购买的商品的ID添加到数组的末尾。 例如: 您的购物车是空的。你打“购买”项目“我的哈利波特” 数组看起来像这样:
array(0 => '4');
现在你为柯南打了“买”:
array(0 => '4', 1 => '1');
...等 0 =&gt;中的第一个数字'4'是自动增量索引,而第二个是您的产品ID。
如果要从数组中删除项目,您需要的是传递索引而不是产品的ID。所以在上面的例子中,如果你想删除“我的哈利波特”,你不会将'4'传递给隐藏字段但是'0'
在你的cart.html.php
中更改此
<?php foreach($cart as $item): ?>
到这个
<?php foreach($cart as $index => $item): ?>
和这个
<input type="hidden" name="delid" id="delid" value="<?php echo $item['id']; ?>">
到这个
<input type="hidden" name="delid" id="delid" value="<?php echo $index; ?>">
index.php中的这段代码
foreach($_SESSION['cart'] as $id) {
foreach($items as $product) {
if($product['id'] == $delid) {
unset($_SESSION['cart'][$delid]);
}
}
}
然后收缩到
unset($_SESSION['cart'][$delid]);
仅
顺便说一下。如果您的购物车将被多个脚本操纵,您还应该在index.php中更改此内容
if(isset($_GET['cart'])) {
$cart = array();
$total = 0;
foreach($_SESSION['cart'] as $id) {
foreach($items as $product) {
if($product['id'] == $id) {
$cart[] = $product;
$total += $product['price'];
break;
}
}
}
include 'cart.html.php';
exit();
}
同步这样的索引
if(isset($_GET['cart'])) {
$cart = array();
$total = 0;
foreach($_SESSION['cart'] as $index => $id) {
foreach($items as $product) {
if($product['id'] == $id) {
$cart[$index] = $product;
$total += $product['price'];
break;
}
}
}
include 'cart.html.php';
exit();
}
请注意,我没有在您的代码中查找任何其他错误