根据会话结果数量回显项目

时间:2017-09-02 08:57:17

标签: php echo

我想根据会话数量多次从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';

感谢任何帮助。

1 个答案:

答案 0 :(得分: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
相关问题