2014-09-28 23 views
0

我試圖呈現特定項目的數據。起初我使用了一個代碼來渲染它的數量和ID。那麼它的工作。只有項目數量呈現到購物車

下面的代碼:

<?php 
//render the cart for the user to view 
$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']; 
      $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>"; 
       } 
      } 
     } 
?> 

但是當我嘗試做出更具體的像只渲染它的編號,名稱,價格和數量。它只能渲染其數量。

這就是:

<?php 
//render the cart for the user to view 
$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']; 
      $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>"; 
      $cartOutput .= "Item ID : " .$each_item['item_id'] . "<br>"; 
      $cartOutput .= "Item Quantity : " .$each_item['quantity'] . "<br>"; 
      $cartOutput .= "Item Name : " .$product_name . "<br>"; 
      $cartOutput .= "Item Price : Php. " .$price . "<br>"; 


      } 
     } 
?> 

有人能指導我在這裏?提前致謝。

+0

可能是問題與變量的範圍有關。您在循環中獲得產品名稱和價格的價值,並在循環時將其指向外側。定義上面的變量while循環就在$ product_name =''的foreach語句之下。希望這會幫助你。 – WisdmLabs 2014-09-28 13:00:09

+0

它不起作用,它給了我通知說,它在53行的/home/a9562316/public_html/cart.php其未定義的變量行 – 2014-09-28 13:06:03

回答

0

我的猜測是,查詢失敗或您使用的字段名稱是錯誤的。嘗試添加這段代碼來檢查兩種可能性。

$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); 

if ($sql === false) { 
    echo 'Query Failed: ' . mysql_error(); 
} 

while($row = mysql_fetch_array($sql)) { 
    print_r($row); // check the field names in this array 

    $product_name = $row["product_name"]; 
    $price = $row["price"]; 

} 

附加SUGGESTION

試試這個,我剛纔看到輸出代碼不是while循環中。並不是說你實際上需要一個while循環,因爲你限制查詢只返回一個結果LIMIT 1

<?php 
//render the cart for the user to view 
$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']; 
     $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); 
     while($row = mysql_fetch_array($sql)){ 
      // no real point in moving these values to another variable 
      // just to print them 
      //$product_name = $row["product_name"]; 
      //$price = $row["price"]; 

      $cartOutput .= "<h2>Cart Item $i</h2>"; 
      $cartOutput .= "Item ID : " . $each_item['item_id'] . "<br>"; 
      $cartOutput .= "Item Quantity : " .$each_item['quantity'] . "<br>"; 
      $cartOutput .= "Item Name : " . $row["product_name"] . "<br>"; 
      $cartOutput .= "Item Price : Php. " . $row["price"] . "<br>"; 
     }  
    } 
} 
?> 
+0

我應該在哪裏添加這個?或者我應該使用這個更改我的代碼? – 2014-09-28 13:07:10

+0

這是添加了幾行代碼的代碼。它應該是相當明顯的。 – RiggsFolly 2014-09-28 13:08:10

+0

哦,謝謝。我試過了,但它只是給了我數量。我的頁面顯示通知說它的未定義變量在product_name和價格在...... blah..blah..blah在第68行和第69行。 – 2014-09-28 13:14:48