Displaying database queries in a odd formation. HOW?????

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
genetix
Forum Contributor
Posts: 115
Joined: Fri Aug 01, 2003 7:40 pm
Location: Sask, Regina
Contact:

Displaying database queries in a odd formation. HOW?????

Post by genetix »

I own a site called Generation-x and I'm making a inventory page where poeple can see all there items. I need to display the items from the database in a odd formation.

The formation has to be:

Three items per line.
After three items are on a line a new <tr> starts.

I was hoping to make it with tables.

Email me at: genetix@generation-x.ca if your reply is to long or you have a premade code for somethign like this.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Something like :

Code: Select all

error_reporting(E_ALL);
$db = mysql_connect('host', 'user' ,'password') or die(mysql_error());
mysql_select_db('thedb') or die(mysql_error());
$sql = "SELECT items FROM inventory";
$result = mysql_query($sql) or die(mysql_error());
$colcount = 0;
echo "<table>\n";
while($row = mysql_fetch_array($result)){
    if($colcount == 0){
        echo "<tr>\n";
    }
    echo '<td>'.$row['item']."</td>\n";
    $colcount++;
    if($colcount == 3){
        echo "</tr>\n";
        $colcount = 0;
    }
}
echo "</table>\n";
I've not added error checking or code to pad out rows that may not have 3 columns (e.g the last row might only have 1 td). There's also other ways to do it using the modulus operator (%) but i've gone for simplicity ;)
User avatar
genetix
Forum Contributor
Posts: 115
Joined: Fri Aug 01, 2003 7:40 pm
Location: Sask, Regina
Contact:

Post by genetix »

thanks. I'll try that. If it doesn't work I got the basic idea so I should be able to rig something up.
Post Reply