1

I want to fetch and echo item from MySQL database multi-times according to session quantity.

Example :

    id  | qty | item

    1  |   2  | shoe

    2  |   4  | net

    3  |   3  | phone

My result set should be like this :

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

I tried :

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';

Any help is appreciated.

ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110

1 Answers1

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