Jump to content

Archived

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

spectsteve7

displaying MySQL results in a table

Recommended Posts

Hi there,

I'm new here and I'm looking for a bit of help. I have a table on my page. It's made of 1 row and two columns. I'm looking to query 1 field from my database and display all records with in the database. I'm looking to make as much use of space as possible so I want to have it populate the data in the first left and right cells then create and populate in a new row. Hopefully someone could point me in the right direction.

Thanks

Share this post


Link to post
Share on other sites
Here's a sample script. Define NUMCOLS as required.
[code]
define ("NUMCOLS", 2);

$res = mysql_query("SELECT columname1, columname2 FROM tablename");

$count = 0;
echo "<TABLE border=1>";
while (list($col1, $col2) = mysql_fetch_row($res)) {

    if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row

    echo "<TD>$col<br>$col2</TD>\n";
    $count++;

    if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
}
if ($count % NUMCOLS != 0) {
   # end row if not already ended
   while ($count++ % NUMCOLS) echo "<td>&nbsp;</td>";
   echo "</TR>\n";
}
echo "</TABLE>";
[/code]

Share this post


Link to post
Share on other sites

×

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.