我已经绞尽脑汁三天了,仍然找不到从会话数组中删除项目的问题的解决方案。
我正在学习 PHP 以及如何构建购物车;我知道有框架,但我真的很想学习编程、解决问题和创造东西。
我的问题是:假设我在购物车中放置了三件商品......
- 饼干
- 甜甜圈
- 纸杯蛋糕
当我删除购物车中的一个项目时,列表中的其他所有内容(该项目下方)也会被删除。如果删除 DONUTS,Cupcakes 也会被删除。如果我删除 Cookie,整个列表将被删除/清除
但是如果我从列表的底部(Cookies)开始,它工作正常;它只会删除我点击的那个。
这是我的代码以及其他尝试。还没有样式,只是在逻辑上工作。
<?php
session_start();
$prodid = $_POST['prodid'];
$quantity = $_POST['quantity'];
$empty= $_POST['empty'];
$removed = $_POST['remove'];
if (isset($empty ))
{
unset($_SESSION['shop_cart']);
}
if (!isset($_SESSION['shop_cart']))//Checking if Session is Set or empty
{
unset($_SESSION['shop_cart']);
echo "Cart is empty";
} else
{
if(count($_SESSION['shop_cart'])==NULL || count($_SESSION['shop_cart'])==0 )
{
echo "Cart is empty";
}
}
/////// THIS IS THE SECTION THAT I'VE BEEN FIDDLING WITH --Am I on the right track
if (isset($removed))
{
foreach ($_SESSION['shop_cart'] as $cart_line_item => $item)
{
if($prodid==$item['prodid'])
{
unset($_SESSION['shop_cart'][$cart_line_item]); ///9:47pm May 20, 2013
break;
}
}
}
///////////////////////////////////////////////////////////////////////////////////
//Check if item is already in cart
foreach ($_SESSION['shop_cart'] as $cart_line_item => $item)
{
if ($item['prodid']==$prodid)
{
$quantity=0;
echo "In Cart Already <br/>";
break;
}
}
$cap=count($_SESSION['shop_cart']);
$_SESSION['shop_cart'][$cap]['prodid']=$prodid;
$_SESSION['shop_cart'][$cap]['quantity']=$quantity;
$_SESSION['shop_cart'][$cap]['description']=$description;
//Prevent Items with zero quantity
foreach ($_SESSION['shop_cart'] as $cart_line_item => $item)
{
if ($item['quantity'] == 0 || $item['quantity'] == NULL)
{
unset($_SESSION['shop_cart'][$cart_line_item]);
break;
}
}
$total=0; //For Price
if (isset($_SESSION['shop_cart']))
{
for($i=0;$i<count($_SESSION['shop_cart']);$i++)
{
$prodid=$_SESSION['shop_cart'][$i]['prodid'];
$quantity=$_SESSION['shop_cart'][$i]['quantity'];
$description=$_SESSION['shop_cart'][$i]['description'];
$query = "SELECT prodid, description, price FROM products WHERE prodid = $prodid";
$result = mysqli_query($hook, $query);
$row = mysqli_fetch_assoc($result);
$prodid = $row['prodid'];
$price = $row['price'];
$description= $row['description'];
$subtotal = $price * $quantity;
$total += $subtotal;
echo "$description($prodid)---Quantity: $quantity--- $$price";
echo "<form action=\"$_SERVER[SCRIPT_NAME]\" method=\"post\" >";
echo "<INPUT TYPE=\"submit\" name=\"remove\" VALUE=\"Remove\">";
echo "<input type=\"hidden\" name=\"prodid\" value=$prodid />\n";
echo "<input type=\"hidden\" name=\"counter\" value=\"$i\"/>\n";
echo "</FORM>";
echo "--------------------------<br/><br/>";
}
}
echo "TOTAL $total <br/><br/>";
echo '<pre>'. var_dump( $_SESSION['shop_cart']).'<pre/>';
echo "<br/>";
echo "<form action=\"$_SERVER[SCRIPT_NAME]\" method=\"post\" >";
echo "<INPUT TYPE=\"submit\" name=\"empty\" VALUE=\"Empty Cart \">";
echo "</FORM>";
?>
该部分代码的这些版本产生相同的结果
一个。
$counter= $_POST['counter'];
echo "COUNTER $counter";
if (isset($removed))
{
unset($_SESSION['shop_cart'][$counter]);
continue;
}
B.
if (isset($removed))
for($i=0;$i<count($_SESSION['shop_cart']);$i++)
if($prodid==$_SESSION['shop_cart'][$i]['prodid'])
{
$_SESSION['shop_cart'][$i]=$quantity==0;
continue;
}
C。
if (isset($removed))
for($i=0;$i<count($_SESSION['shop_cart']);$i++)
if($prodid==$_SESSION['shop_cart'][$i]['prodid'])
{
unset($_SESSION['shop_cart'][$i]['prodid']);
//OR
//unset($_SESSION['shop_cart'][$i]);
continue;
}
这根本不起作用
if (isset($removed))
{
foreach ($_SESSION['shop_cart'] as $cart_line_item => $item)
{
if($prodid==$item['prodid'])
{
unset($_SESSION['shop_cart']['prodid']);
unset($_SESSION['shop_cart']['quantity']);
unset($_SESSION['shop_cart']['description']);
continue;
}
}
}
对于@Soyale:
你是这个意思吗?:
if (isset($removed))
{
foreach ($_SESSION['shop_cart'] as $cart_line_item => $item)
{
if($prodid==$item['prodid'])
{
unset($_SESSION['shop_cart'][$cart_line_item]);
continue;
}
}
}
结果相同。