【发布时间】:2013-07-27 20:48:01
【问题描述】:
我在一个网站上设置了一个购物车,它使用一个名为 cart.php 的文件来完成所有工作。 “添加”功能完美运行,我也可以清空购物车,但不能删除单个项目
删除链接如下所示:
<a href='cart.php?action=delete&id=$cartId'>delete</a>
它会创建一个类似这样的链接:cart.php?action=delete&id=1
文件cart.php在这里:
<?php
require_once('Connections/ships.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
$items = explode(',',$cart);
if (count($items) > 5)
{
header("Location: shipinfo_full.php") ;
}
else
{
switch ($action)
{
case 'add':
if ($cart)
{
$cart .= ','.$_GET['ship_id'];
}
else
{
$cart = $_GET['ship_id'];
}
header("Location: info_added.php?ship_id=" . $_GET['ship_id']) ;
break;
case 'delete':
if ($cart)
{
$items = explode(',',$cart);
$newcart = '';
foreach ($items as $item)
{
if ($_GET['ship_id'] != $item)
{
if ($newcart != '')
{
$newcart .= ','.$item;
}
else
{
$newcart = $item;
}
}
}
$cart = $newcart;
}
header("Location: info.php?ship_id=" . $_GET['ship_id']) ;
break;
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
}
?>
有什么想法可以删除单个项目吗?
【问题讨论】:
-
你的命名是否一致?在 OP 中,您将其称为“id”,但随后您的代码正在寻找“ship_id”...
-
嗨,这不是 Stack Overflow 应该如何工作的。你应该带来一些的知识和努力。据我所知,这是您复制和粘贴代码的第四个问题,本质上是要求社区为您完善和修复它。
-
感谢汤姆 - 真的很有帮助! Pekka,在我做事的同时努力学习,所以如果你想建议我尝试的地方,很乐意倾听