Page 1 of 1

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

Posted: Sat Oct 18, 2003 10:13 am
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.

Posted: Sat Oct 18, 2003 10:25 am
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 ;)

Posted: Sat Oct 18, 2003 11:06 am
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.