すべての作業を行うために cart.php というファイルを使用する Web サイトにショッピング カートをセットアップしました。「追加」機能は完全に機能し、カートを空にすることもできますが、単一のアイテムを削除することはできません
削除リンクは次のようになります。
<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;
}
?>
単一のアイテムを削除する方法はありますか?