2017-09-02 52 views
1

我想根據會話數量從MySQL數據庫中多次取回項目。根據會話結果數量回顯項目

例子:

id | qty | item 

    1 | 2 | shoe 

    2 | 4 | net 

    3 | 3 | phone 

我的結果集應該是這樣的:

shoe, shoe 
net, net, net, net 
phone, phone, phone 

我想:

if(isset($_SESSION["cart_products"])) 
    foreach ($_SESSION["cart_products"] as $each_item){ 
     $item_id = $each_item['item_id']; 
    $qty = $each_item['quantity']; 
     $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' ORDER BY id"); 
     $qty = array(); 
    while ($row = mysql_fetch_array($sql)) { 
     $qty[] = $row; 
    }foreach($qty as $row){ 
     $product_name = $row["product_name"]; 
     } 
echo'$product_name'; 

任何幫助表示讚賞。

回答

0
if(isset($_SESSION["cart_products"])) 
    foreach ($_SESSION["cart_products"] as $each_item){ 
     $item_id = $each_item['item_id']; 
     $qty = $each_item['quantity']; 
     $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' 
       ORDER BY id"); 
     while ($row = mysql_fetch_array($sql)) { 
      $qty = $row['qty']; 
      for($i=0; $i < $qty; $i++){ 
       echo $row['item'].' ';  
      }//end for 
      echo '<br>';//go to next-line  
     }//end while 
}//end foreach 
+0

$ qty是一個會話結果而不是數據庫結果,它在數據庫中沒有culumn它應該這樣嗎? <?php session_start(); 包含「mysql.php」;如果(!isset($ _ SESSION [「cart_array」])|| count($ _ SESSION [「cart_array」])<1) ?> <?php echo「shopping is empty」; } else foreach($ _ SESSION [「cart_array」] as $ each_item){ $ item_id = $ each_item ['item_id']; $ qty = $ each_item ['quantity']; $ sql = mysql_query(「SELECT * FROM products WHERE id ='$ item_id' ORDER BY id」); while($ row = mysql_fetch_array($ sql)){ for($ i = 0; $ i <$ qty; $ i ++){ $ y = $ row ['product_name']; } echo'$ y'; } } } ?> –