[英]how do i echo the total item quantity from a php session based array.
i cant for the life of me understand how i get the total amount of items. 我无法终生了解我如何获得全部物品。 i've been following this tutorial to set up a webshop.
我一直在按照本教程设置网上商店。 http://jameshamilton.eu/content/simple-php-shopping-cart-tutorial
http://jameshamilton.eu/content/simple-php-shopping-cart-tutorial
and everything seems to work within the tutorial. 一切似乎都可以在本教程中使用。 But i need to add the total number of items to my shopping cart.
但是我需要将商品总数添加到我的购物车中。 the code is creating an array with an id and quantity-number as an array stored within SESSION['cart'] .
代码正在创建一个ID和数量为数字的数组作为存储在SESSION ['cart']中的数组。 i have been messing around with the FOREACH code, but i only either get the total number of one of the items in the cart, og the total number of arrays.
我一直在摸索着FOREACH代码,但是我只能获得购物车中一项的总数,也可以是数组的总数。 but i need the sum of the total quantity numbers, not the sum of rows.
但我需要总数总数,而不是行总数。
any help in the right direction is much appreciated. 朝着正确方向提供的任何帮助均深表感谢。
the working code: 工作代码:
$product_id = $_GET[id]; //the product id from the URL
$action = $_GET[action]; //the action from the URL
if($product_id && !productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
switch($action) { //decide what to do
case "add":
$_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
break;
case "remove":
$_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]);
break;
case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
break;
}
if($_SESSION['cart']) { //if the cart isn't empty
//show the cart
echo "<table border='1' padding=\"3\" width=\"40%\">";
foreach($_SESSION['cart'] as $product_id => $quantity) {
$sql = sprintf("SELECT productName, productImg, price FROM products WHERE id = %d;", $product_id);
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
list($productName, $productImg, $price) = mysql_fetch_row($result);
$arrayquantity = is_array($_SESSION['cart']) ? count($_SESSION['cart']) : 0;
$line_cost = $price * $quantity; //work out the line cost
$total = $total + $line_cost; //add to the total cost
}else{
//you have no items
}
function productExists($product_id) {
$sql = sprintf("SELECT * FROM products WHERE id = %d;", $product_id);
return mysql_num_rows(mysql_query($sql)) > 0;
}
i have tried the following but it just results in "0" 我尝试了以下操作,但结果为“ 0”
if(isset($_SESSION['cart']) AND is_array(@$_SESSION['cart'])){
foreach($_SESSION['cart'] AS $itemquantity){
$totalquantity = $totalquantity + $itemquantity['quantity'];
}
}
else{
$totalquantity = 0;
}
echo $totalquantity;
Try something like: 尝试类似:
if(isset($_SESSION['cart']) && is_array($_SESSION['cart'])) {
$totalquantity = 0;
foreach($_SESSION['cart'] AS $productId => $itemQuanity) {
$totalquantity = $totalquantity + $itemQuanity;
}
}
else {
$totalquantity = 0;
}
echo $totalquantity;
You can replace foreach($_SESSION['cart'] AS $productId => $itemQuanity){ with foreach($_SESSION['cart'] AS $itemQuanity){ because you dont have need for key(here key is product id). 您可以将foreach($ _ SESSION ['cart'] AS $ productId => $ itemQuanity){替换为foreach($ _ SESSION ['cart'] AS $ itemQuanity){因为您不需要密钥(这里的密钥是产品ID) 。
couple of changes: AND
is logical, u need to use &&
for conditional check. 几处更改:
AND
是合乎逻辑的,您需要使用&&
进行条件检查。 also, is_array()
shouldn't have a @
in the parameter. 同样,
is_array()
参数中不应包含@
。
if(isset($_SESSION['cart']) && is_array($_SESSION['cart'])){ //change this line
$totalquantity = 0;
foreach($_SESSION['cart'] AS $itemquantity){
$totalquantity += $itemquantity['quantity']; // and this line, just shorthand of ur line
}
}
else{
$totalquantity = 0;
}
echo $totalquantity;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.