2013-04-13 140 views
0

我已經編碼了我的購物車頁面,它似乎沒有顯示任何東西。以前在我的產品頁面上,我有一個表單操作,點擊添加到購物車後,會導致我到cart.php。我不確定這裏出了什麼問題。購物車頁面沒有顯示任何東西

<?php 
session_start(); 
//script error reporting 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
// Connect to the MySQL database 
include "storescripts/connect.php"; 
?> 

<?php 
if (isset($_POST['pid'])) { 
$pid = $_POST['pid']; 
$wasFound = false; 
$i = 0; 
// if the cart session variable is not set or cart array is empty 
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
    // run if the cart is empty or not set 
    $_SESSION["cart_array"] = array(1 => array("item_id" => $pid, "quantity" => 1)); 
} else { 
    // run if the cart has at least one item in it 
    foreach ($_SESSION["cart_array"] as $each_item) { 
     $i++; 
     while (list($key, $value) = each($each_item)) { 
      if ($key == "item_id" && $value == $pid) { 
       //that item is in cart already so let's adjust its quantity using array_slice() 
       array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1))); 
       $wasFound = true; 
      } //close if condition 
     }//close while loop 
    }//close foreach loop 
    if ($wasFound == false) { 
     array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1)); 
    } 
    } 
} 
?> 
<?php 
// SECTION 2(if user chooses to empty their shopping cart) 
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { 
unset($_SESSION["cart_array"]); 
} 
?> 
<?php 
// SECTION 3 (render the cart for the user to view) 
$cartOutput = ""; 
if (!isset($_SESSION["crt_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"]; 
    $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); 
    while ($row = mysql_fetch_array($sql)) { 
     $product_name = $row["product_name"]; 
     $price = $row["price"]; 
    } 
    $cartOutput .= "<h2>Cart Item $i</h2>"; 
    //while(list($key, $value) = each($each_item)) 
      // $cartOutput .= "$key: $value<br/>"; 
    $cartOutput .= "Item ID:" . $each_item['item_id'] . "<br/>"; 
    $cartOutput .= "Item Quantity:" . $each_item['quantity'] . "<br/>"; 
    $cartOutput .= "Item Name:" . $product_name . "<br/>"; 
    $cartOutput .= "Item Price: $" . $price . "<br/>"; 
    } 
} 
?> 
+0

已解決!感謝您抽出寶貴時間,請不要投下來,這個網站是一個很好的幫助,並希望能夠幫助其他領域的人好吧,謝謝(: – nur

+0

不要忘了接受答案,如果它幫助。 – 1337holiday

回答

0

你有一個小的錯誤。拼寫錯誤,親愛的。它發生了。人爲錯誤總是會發生(:和它在會話中的數組():(

+0

謝謝謝謝!它肯定會。@nurf – nur

相關問題