代码之家  ›  专栏  ›  技术社区  ›  Faith Aniefiok

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

  •  -3
  • Faith Aniefiok  · 技术社区  · 7 年前

    如何使用每个不同的查询示例多次从mysql数据库回显同一项,如果

    catt |数量|名称|代码

    WAP | 3 | shoe |$查询

    提示| 3 | phon |$查询

    我的结果集应该是这样的:

    WAP鞋113zu1

    WAP鞋125dj1

    WAP鞋125332

    TIP phon dej21

    TIP phon 5waja7p2

    TIP电话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 回复  |  直到 7 年前
        1
  •  1
  •   tdoggy    7 年前

    请花时间研究准备好的报表。PDO将使你免于头痛。此外,MySQLi>MySQL。

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