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.
Displaying database queries in a odd formation. HOW?????
Moderator: General Moderators
Something like :
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 
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";