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