我一直在絞盡腦汁三天,仍然找不到解決我從會話數組中刪除項目的問題。刪除會話數組中的項目
我正在學習PHP以及如何構建購物車;我知道有框架,但我真的很想學習編程,解決問題並創建事物。
我的問題是這樣的:說我放在車三個項目...
- 餅乾
- 甜甜圈
- 蛋糕
當我在購物車中刪除項目,列表中的所有其他項目(在該項目下方)也會被刪除。 如果刪除DONUTS,紙杯蛋糕也被刪除。 如果我刪除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>";
?>
這些版本代碼產率相同的結果的那個部分的
A.
$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;
}
}
}
相同的結果。
的http://stackoverflow.com/questions/2231332/remove-a-variable-from-a-php-session-array –
可能重複我見過該帖子並非重複;在某些方面類似但不相同。我也試過這個帖子的答案,他們不工作;有些甚至與我的結果相同。 – Patty