2014-09-28 3 views
0

특정 항목에 대한 데이터를 렌더링하려고합니다. 처음에는 수량과 ID를 렌더링하는 코드를 사용했습니다. 잘 그 일.ITEM QUANTITY 만 장바구니에 표시됩니다.

여기에 코드입니다 :

<?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>"; 
       } 
      } 
     } 
?> 

그러나 나는 단지의 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>"; 
      $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

변수의 범위가 문제가 될 수 있습니다. 당신은 제품 이름과 가격의 가치를 루프 안에서 그리고 내부에서 루프를 참조하면서 값을 얻고 있습니다. 위의 변수를 $ product_name = ''처럼 foreach 문 바로 아래에 정의하십시오. 희망이 당신을 도울 것입니다. – WisdmLabs

+0

그것이 작동하지 않는다 그것은 내게주는 알림 그것의 정의되지 않은 변수 행 /home/a9562316/public_html/cart.php 온라인 53 –

답변

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"]; 

} 

대신이 시도 추가 제안, 난 그냥 출력 코드는 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

어디에 내가 이것을 추가해야합니까? 아니면 이것을 사용하여 코드를 변경해야합니까? –

+0

이것은 두 줄을 추가 한 코드입니다. 그것은 아주 분명해야합니다. – RiggsFolly

+0

오 감사합니다. 나는 노력했지만 아직도 나에게 양을 준다. 그리고 내 페이지는 product_name에 정의되지 않은 변수를 말하는 Notice를 보여주고 68 번과 69 번 줄에는 blah..blah..blah의 값을 나타냅니다. –

관련 문제