2016-02-02 290 views
1

因此,我設置了我的購物車,以使用GET功能接收有關添加的每個項目的特定信息。但是,在讀取數據庫的實現中,如果添加另一個項目,這些值將會變得相同。 如果我添加椅子1,然後再次椅子1,它增加了椅子1的總數,說有2椅子1的。但是,如果我再添加椅子2,則會有一個新的條目,但具有所有椅子的價值。PHP購物車陣列

陣列輸出

Array ([0] => Array ([item_id] => 2 [quantity] => 1) [1] => Array ([item_id] => 4 [quantity] => 7)) 

採購項目區

enter image description here

數據庫:

<?php 
include_once('config/database.php'); 
include_once('object/chair.php'); 
$database = new Database(); 
$conn = $database->getConnection(); 
$chair = new Chair($conn); 
$chair->id = $_GET['detailsid']; 
$stmt = $chair->readDetails(); 
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ 
?> 

添加到購物車:

<div class="addtocart"> 
       <!--<div class="button"> 
       Add to cart 

       </div>--> 
       <div class="button"> 
       <form id="form1" name="form1" method="post" action="cart.php?detailsid=<?php echo $row['ID'];?>"> 
      <input type="hidden" name="pid" id="pid" value="<?php echo $row['ID'];?>"/> 
      <input type="submit" name="button" id="button" value="Add to Shooping Cart"/>   

        </form> 

車溫控功能:

<?php 

session_start(); 
error_reporting(E_ALL); 
ini_set('display_errrors', '1'); 
include_once 'includes/db_conx.php'; 
if (isset($_POST['pid'])) 
{ 
    $pid  = $_POST['pid']; 
    $wasFound = false; 
    $i  = 0; 
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) 
    { 
     $_SESSION["cart_array"] = array(1 => array("item_id" => $pid, "quantity" => 1)); 
    } 
    else 
    { 
     foreach ($_SESSION["cart_array"] as $each_item) 
     { 
      $i++; 
      while (list($key, $value) = each($each_item)) 
      { 
       if ($key == "item_id" && $value == $pid) 
       { 
        array_splice($_SESSION["cart_array"], $i - 1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1))); 
        $wasFound = true; 
       } 
      } 
     } 
     if ($wasFound == false) 
     { 
      array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1)); 
     } 
    } 
} 
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") 
{ 
    unset($_SESSION["cart_array"]); 
} 

//render cart 
$cartOutput = ""; 
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) 
{ 
    $cartOutput = "<h2 align=center'>Your shopping cart is empty</h2>"; 
} 
else 
{ 
    $i = 0; 
    foreach ($_SESSION["cart_array"] as $each_item) 
    { 
     $i++; 
     $item_id = $each_item['item_id']; 

     include_once('config/database.php'); 
     include_once('object/chair.php'); 
     $database = new Database(); 
     $conn  = $database->getConnection(); 
     $chair  = new Chair($conn); 
     $chair->id = $_GET['detailsid']; 
     $stmt  = $chair->readDetails(); 
     while ($row  = $stmt->fetch(PDO::FETCH_ASSOC)) 
     { 
      $product_name = $row['chair_name']; 
      $price  = $row['PRICE']; 
     } 

     $pricetotal = $price * $each_item['quantity']; 
     $cartOutput .="<tr>"; 
     $cartOutput .= "<td>" . $product_name . "</td>"; 
     $cartOutput .= "<td>" . $price . "</td>"; 
     $cartOutput .= "<td>" . $each_item['quantity'] . "</td>"; 
     $cartOutput .= "<td>" . $pricetotal . "</td>"; 
     $cartOutput .= "<td>X</td>"; 
     $cartOutput .="</tr>"; 
    } 
} 
+0

顯示處理將項目添加到'$ _SESSION ['cart_arrray']' – Fabricator

+0

的代碼請顯示$ _SESSION [「cart_array」]的代碼 - 它可能會在下面創建一個新項目而不是合併數量,因爲數組鍵是diff – rubberchicken

+0

好的沒問題 –

回答

1

更新:

一個可能的修復可能是細節ID添加到會話陣列像這樣:

if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) 
{ 
    $_SESSION["cart_array"] = array(1 => array("item_id" => $pid, "quantity" => 1, "details_id" => $_GET['detailsid'])); 
} 
else 
{ 
    foreach ($_SESSION["cart_array"] as $each_item) 
    { 
     $i++; 
     while (list($key, $value) = each($each_item)) 
     { 
      if ($key == "item_id" && $value == $pid) 
      { 
       array_splice($_SESSION["cart_array"], $i - 1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1, "details_id" => $_GET['detailsid']))); 
       $wasFound = true; 
      } 
     } 
    } 
    if ($wasFound == false) 
    { 
     array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1, "details_id" => $_GET['detailsid'])); 
    } 
} 

然後顯示購物車使用時該值代替$_GET一個:

$chair  = new Chair($conn); 
$chair->id = $each_item['details_id']; 
$stmt  = $chair->readDetails(); 

您正在使用提取的$_GET['detailsid']椅子的細節 - 這將永遠是每個迴路編號相同:

$chair  = new Chair($conn); 
$chair->id = $_GET['detailsid']; 
$stmt  = $chair->readDetails(); 

如果這是$item_id代替你獲取正確的椅子的細節?

+0

是的,我有點想了很多,但我不知道如何通過除GET以外的變種。該ID是sql文件中每個項目的唯一標識符。雖然$ item_id僅適用於購物車中的每件商品,因此您可以刪除每件商品。當「添加到購物車」按鈕被擊中時,ID被作爲正在發送給detailid。 –

+0

您能否將詳細信息與其他詳細信息一起添加到購物車陣列中?'array_push($ _ SESSION [「cart_array」],array(「item_id」=> $ pid,「quantity」=> 1,「detailsid」=> 99));' –

+0

我已更新我的回答,更多詳細信息 –