Jump to content

displaying MySQL results in a table


spectsteve7

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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.