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 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号