bambinou1980 Posted December 30, 2015 Share Posted December 30, 2015 Hello, I am trying to construct my first while loop and struggle a bit. I have a table with 4 columns, all I am trying to do is passing the product name, qty, price and vat in each column from the same table but cannot work out how to use only 1 loop rather than 4 because the html is on the way.... Any idea how I could compress the lot to make it only in 1 loop please? <?php $query1 = "SELECTo.orders_id,p.product_name,p.idFROM orders AS oLEFT JOIN products_ordered AS p ON o.orders_id = p.orders_orders_id WHERE o.orders_id = {$order_id}ORDER BY p.id DESC"; $result1 = mysqli_query($connection, $query1); if($result1 && mysqli_affected_rows($connection) < 1) { // Failure // $message = "Subject update failed"; die("Database query 1 failed. " . mysqli_error($connection));}?> <?php $query2 = "SELECTo.orders_id,p.product_quantity,p.idFROM orders AS oLEFT JOIN products_ordered AS p ON o.orders_id = p.orders_orders_id WHERE o.orders_id = {$order_id}ORDER BY p.id DESC"; $result2 = mysqli_query($connection, $query2); if($result2 && mysqli_affected_rows($connection) < 1) { // Failure // $message = "Subject update failed"; die("Database query 2 failed. " . mysqli_error($connection));}?> <?php $query3 = "SELECTo.orders_id,p.product_price,p.idFROM orders AS oLEFT JOIN products_ordered AS p ON o.orders_id = p.orders_orders_id WHERE o.orders_id = {$order_id}ORDER BY p.id DESC"; $result3 = mysqli_query($connection, $query3); if($result3 && mysqli_affected_rows($connection) < 1) { // Failure // $message = "Subject update failed"; die("Database query 3 failed. " . mysqli_error($connection));}?> <?php $query4 = "SELECTo.orders_id,p.product_vat,p.idFROM orders AS oLEFT JOIN products_ordered AS p ON o.orders_id = p.orders_orders_id WHERE o.orders_id = {$order_id}ORDER BY p.id DESC"; $result4 = mysqli_query($connection, $query4); if($result4 && mysqli_affected_rows($connection) < 1) { // Failure // $message = "Subject update failed"; die("Database query 4 failed. " . mysqli_error($connection));}?> <!-- / end client details section --> <table class="table table-bordered"> <thead> <tr> <th> <h4 class="text-center">Products Ordered</h4> </th> <th> <h4 class="text-center">Qty</h4> </th> <th> <h4 class="text-center">Products Prices</h4> </th> <th> <h4 class="text-center">V.A.T</h4> </th> </tr> </thead> <tbody> <tr> <td><div class="text-center"><h4> <?php while ($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) { $product_name = $row["product_name"]; echo $product_name . '<br>';} ?> </h4></div></td> <td><div class="text-center"><h4> <?php while ($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)) { $product_quantity = $row["product_quantity"]; echo $product_quantity . '<br>';} ?> </h4></div></td> <td><div class="text-center"><h4> <?php while ($row = mysqli_fetch_array($result3, MYSQLI_ASSOC)) { $product_price = $row["product_price"]; echo "€" .$product_price . '<br>';} ?> </h4></div></td> <td><div class="text-center"><h4> <?php while ($row = mysqli_fetch_array($result4, MYSQLI_ASSOC)) { $product_vat = $row["product_vat"]; echo $product_vat . '%<br>';} ?> </h4></div></td> </tr> </tbody> </table> Quote Link to comment Share on other sites More sharing options...
Solution bambinou1980 Posted December 30, 2015 Author Solution Share Posted December 30, 2015 Ok Worked it out: <?php while ($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) { $product_name = $row["product_name"]; $product_quantity = $row["product_quantity"]; $product_price = $row["product_price"]; $product_vat = $row["product_vat"]; ?><tr><td><div class="text-center"><h4><?php echo $product_name; ?></h4></div></td><td><div class="text-center"><h4><?php echo $product_quantity; ?></h4></div></td><td><div class="text-center"><h4><?php echo $product_price; ?></h4></div></td><td><div class="text-center"><h4><?php echo $product_vat; ?></h4></div></td></tr><?php } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.