dqba94394 2017-09-08 07:53
浏览 76
已采纳

php根据每个数量多次回显相同的变量

How to echo same item from mysql database multiple times with each diffrent query example if

catt | qty | name | code

WAP | 3 | shoe | $query

BOY | 2 | net | $query

TIP | 3 | phon | $query

My result set should be like this: echo

WAP shoe 113zu1

WAP shoe 125dj1

WAP shoe 125332

BOY net 11331

BOY net 13wa2

TIP phon dej21

TIP phon 5waja7p2

TIP phon 532j3

i try <?php 
session_start();
include "mysql.php"; 
?>
<?php 
$cartOutput = "";
$product_id_array = '';
if (isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) > 1) { 
  // Start the For Each loop
   $i = 0; 
    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)) {
       $product_name = $row["product_name"];
// Create the product array variable
      $product_id_array .= "$item_id-".$each_item['quantity'].","; 
      // Dynamic table row assembly
  for ($b = 1; $b <= $each_item['quantity']; $b++) {
         $cartOutput .= "<tr>";
      $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
      //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
      $cartOutput .= '</tr>';
      $i++; 
    }}
?>
    //output each variable with diffrent code from fetch query
      <?php $query_select = ("SELECT code, FROM diffrent_codes WHERE name='$product_name' ORDER BY id ASC"); $result_select = $mysqli_query($query_select) or die(mysql_error()); $rows = array(); while($row = mysql_fetch_array($result_select)) $rows[] = $row; foreach($rows as $row){ $code = $row['code']; } echo $cartOutput$code; } ?

 ?>
my $cartOutput$code to output diffrent $code according to each session quantity variable
  • 写回答

1条回答 默认 最新

  • duandian8110 2017-09-08 09:16
    关注

    Please invest time to looking into prepared statements. PDO will save you from headaches. Also, MySQLi > MySQL.

    Now, the reason you don't echo your content multiple times is because you have it set up to echo the row only once. You need a for loop within your while loop that executes as many times as there is quantity. I'll edit with an example later.

    $sql = $pdo->prepare("SELECT * FROM `products` WHERE `id` = :item_id ORDER BY `id`");
    $sql->bindValue(":item_id", $item_id, PDO::PARAM_INT);
    
    if ($sql->execute()) {
        foreach ($sql->fetchAll(PDO::FETCH_ASSOC) as $row) {
            $product_name = $row["product_name"];
            // Dynamic table row assembly
    
            for ($i = 1; $i <= $row['quantity']; $i++) {
                $cartOutput .= "<tr>";
                $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
                //$cartOutput .= '<td>' . $row['quantity'] . '</td>';
                $cartOutput .= '</tr>';
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用
  • ¥15 ESP-IDP-BLE配网连接wifi
  • ¥15 ue2.6.12版本用的若以,安装gojs,引入import * as go from 'gojs';报错
  • ¥15 服务器上的网站安装php5.6版本
  • ¥15 ModuleNotFoundError: No module named 'torch.utils._import_utils' 是缺少什么
  • ¥15 请大咖一起探索iptv 直播源的hls通过反向代理解密
  • ¥100 寻找技术员 云闪付tn转h5输入卡号付款的链接 重酬!