Jump to content

Retrieving MySQL info and putting it into 3 columns


3motions

Recommended Posts

Hey guys..

 

I appreciate all of the help you guys have given me in getting this website of mine to work correctly. :) It's definitely appreciated.. Now, the next question I have for you is I want to pull out products that I have in my MySQL database. But like most of the things I display on my website, I don't want it to show up just in a list that goes straight down. I want to arrange everything that I pull out of the database into a table with 3 columns and however many rows it takes to display everything.

 

So, in simpler terms.. Instead of displaying like this:

 

- Gift Card

- Playstation

- Pizza

- Coffee

- Shoes

- Clothes

 

I want it to display like this:

 

Gift Card      Playstation    Pizza

Cofee          Shoes            Clothes

 

Can anyone help me out with this? I've tried searching on Google, but I can't quite get the terminology down right so I can't really find the right code.

 

Thanks!

<?php
include 'db.php';

define ("NUMCOLS",3);

$res = mysql_query("SELECT col1, col2 FROM mytable");

$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>$col1<br>$col2</TD>\n";
    $count++;

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

# end row if not already ended

if ($count % NUMCOLS != 0) {
   while ($count++ % NUMCOLS) echo "<td> </td>";
   echo "</TR>\n";
}
echo "</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.