我已经绞尽脑汁待了三天,仍然无法找到解决我从会话数组中删除项目的问题的解决方案。
我正在学习PHP以及如何构建购物车;我知道有框架,但我真的想学习编程,解决问题和创造事物。
我的问题是这样的:假设我在购物车中放了三件物品......
当我删除购物车中的商品时,列表中的所有其他内容(在该商品下方)也会被删除。 如果删除DONUTS,也会删除纸杯蛋糕。 如果我删除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;
}
乙
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;
}
下进行。
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;
}
}
}
同样的结果。
答案 0 :(得分:0)
你的问题在于:
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/>";
}
}
你使用foreach代替。为什么? 因为在使用unset时删除了键。 基本数组,即
array (size=2)
0 =>
array (size=3)
'prodid' => string '1' (length=1)
'quantity' => string '2' (length=1)
'description' => null
1 =>
array (size=3)
'prodid' => string '2' (length=1)
'quantity' => string '2' (length=1)
'description' => null
2 =>
array (size=3)
'prodid' => string '3' (length=1)
'quantity' => string '2' (length=1)
'description' => null
删除带prodid=2
的产品后,如下所示:
array (size=2)
0 =>
array (size=3)
'prodid' => string '1' (length=1)
'quantity' => string '2' (length=1)
'description' => null
2 =>
array (size=3)
'prodid' => string '3' (length=1)
'quantity' => string '2' (length=1)
'description' => null
决赛桌的长度是2.当您使用类似for($i=0;$i<count($_SESSION['shop_cart']);$i++)
的内容时,第二个产品将更新显示(因为键)。更多的是没有索引为1的行,所以empy行将放在那里。
看看这一行:
$total=0; //For Price
if (isset($_SESSION['shop_cart']))
{
for($i=0;$i<count($_SESSION['shop_cart']);$i++) //-> this is a bad idea please change this to foreach
{