0

アイテムの合計量を取得する方法を理解することはできません。私はこのチュートリアルに従ってウェブショップを立ち上げました。http://jameshamilton.eu/content/simple-php-shopping-cart-tutorial

チュートリアル内ではすべてが機能しているようです。ただし、アイテムの総数をショッピングカートに追加する必要があります。コードは、SESSION['cart']内に格納されている配列としてidとquantity-numberを持つ配列を作成しています。私はFOREACHコードをいじっていますが、配列の総数に対して、カート内のアイテムの1つの総数のみを取得します。しかし、行の合計ではなく、合計数量数の合計が必要です。

正しい方向への助けは大歓迎です。

作業コード:

$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;
}

次のことを試しましたが、結果は「0」になります。

if(isset($_SESSION['cart']) AND is_array(@$_SESSION['cart'])){
   foreach($_SESSION['cart'] AS $itemquantity){
   $totalquantity = $totalquantity + $itemquantity['quantity'];
   }
}
else{
$totalquantity = 0;
}
echo $totalquantity;
4

2 に答える 2

1

次のようなものを試してください:

  if(isset($_SESSION['cart']) && is_array($_SESSION['cart'])) {
        $totalquantity = 0;
        foreach($_SESSION['cart'] AS $productId => $itemQuanity) {
            $totalquantity = $totalquantity + $itemQuanity;
        }
  }
  else {
       $totalquantity = 0;
  }
  echo $totalquantity;

キーが必要ないため、 foreach($_SESSION['cart'] AS $productId => $itemQuanity){ を foreach($_SESSION['cart'] AS $itemQuanity){ に置き換えることができます(ここでのキーは製品IDです) .

于 2012-09-26T18:35:40.593 に答える
0

いくつかの変更:AND論理的&&です。条件付きチェックに使用する必要があります。また、パラメータ に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;
于 2012-09-26T18:16:03.737 に答える