Jump to content

[SOLVED] How to display MySQL table data in a table


whiskedaway

Recommended Posts

I'm really stuck, so I'm hoping someone might be able to help. I'm using PHP5 and I can't work out how to get each different row to display. Currently the first row displays over and over again until there are no more rows left in the table.

 

So far this is what I've done:

<?php

include("credentials.inc");

$cxn = mysqli_connect($host, $user, $password, $dbname) 
           or die ("Connection failed.");

$sql = "SELECT o.orderID, i.sku, i.quantity, o.staffID FROM Orders o, ItemsOrdered i
        WHERE o.orderID = i.orderID";	
$result = mysqli_query($cxn, $sql)
              or die ("Couldn't execute query.");
    $num = mysqli_num_rows($result);
$rowname = mysqli_fetch_assoc($result);
    extract ($rowname);

?>

<table width="250" border="0" align="center" cellpadding="2" cellspacing="2">
<tr><td><strong>OrderID</strong></td><td><strong>SKU</strong></td><td><strong>Quantity</strong></td><td><strong>StaffID</strong></td></tr>

<?php
$i=0;
while ($i < $num) {
    //something here to change the values of $orderID, $sku, $quantity and $staffID
?>

<tr><td><?php echo $orderID; ?></td><td><?php echo $sku; ?></td><td><?php echo $quantity; ?></td><td><?php echo $staffID; ?></td></tr>

<?php
$i++;
}
?>

</table>

 

Hope someone can help!

this should work

 

<?php

   include("credentials.inc");
      
   $cxn = mysqli_connect($host, $user, $password, $dbname) 
           or die ("Connection failed.");
   
   $sql = "SELECT o.orderID, i.sku, i.quantity, o.staffID FROM Orders o, ItemsOrdered i
           WHERE o.orderID = i.orderID";   
   $result = mysqli_query($cxn, $sql)
              or die ("Couldn't execute query.");
    $num = mysqli_num_rows($result);
   
?>

<table width="250" border="0" align="center" cellpadding="2" cellspacing="2">
<tr><td><strong>OrderID</strong></td><td><strong>SKU</strong></td><td><strong>Quantity</strong></td><td><strong>StaffID</strong></td></tr>

<?php
while ($rowname = mysqli_fetch_assoc($result)) {
    extract ($rowname);

    //something here to change the values of $orderID, $sku, $quantity and $staffID
?>

<tr><td><?php echo $orderID; ?></td><td><?php echo $sku; ?></td><td><?php echo $quantity; ?></td><td><?php echo $staffID; ?></td></tr>

<?php
}
?>

</table>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.