Forum Moderators: coopster

Message Too Old, No Replies

Displaying mysql results in a table

         

nfs2

9:27 am on Apr 5, 2006 (gmt 0)

10+ Year Member



So im pulling information from a database, i'd like to display it in a table no longer then 6 columns, and as many rows long as there is data.

For example, here's the code im using now, which displays an unlimited number of columns on only one row

$result7=mysql_query("SELECT journal_id, MAX(entry_timestamp) AS entry_timestamp FROM journal_entries GROUP BY journal_id ORDER BY entry_timestamp DESC");

while ($i = mysql_fetch_array($result7)) {
$mainpic = mainpic($i['journal_id']);
$time = backwhen($i['entry_timestamp']);
$name = truncate($i['journal_id']);

echo '<td style="padding: 10px; border: 1px solid #0056a1; vertical-align: top; background: url(\'topblue.jpg\') repeat-x;"><center><b><a href="http://www.example.com/profiles/'.$i['journal_id'].'">'.$name.'</a></b><br/><br/><a href="http://www.example.com/users/'.$i['journal_id'].'">'.$mainpic.'</a><div style="color: red; font-size: 9px;">'.$time.'&nbsp;ago</div></center></td><td width="5px"></td>';
}

So this displays a very long table row of many td's. I'd like help figuring out a way to put some <tr>'s in there after 7 td's

Does anyone know how to do this?

tomda

10:02 am on Apr 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$qty="7";

$result7=mysql_query("SELECT journal_id, MAX(entry_timestamp) AS entry_timestamp FROM journal_entries GROUP BY journal_id ORDER BY entry_timestamp DESC");

$nb="0";
while ($i = mysql_fetch_array($result7)) {
$mainpic = mainpic($i['journal_id']);
$time = backwhen($i['entry_timestamp']);
$name = truncate($i['journal_id']);

if($nb % $qty == "0") {echo '</tr><tr>';}

echo '<td style="padding: 10px; border: 1px solid #0056a1; vertical-align: top; background: url(\'topblue.jpg\') repeat-x;"><center><b><a href="http://www.example.com/profiles/'.$i['journal_id'].'">'.$name.'</a></b><br/><br/><a href="http://www.example.com/users/'.$i['journal_id'].'">'.$mainpic.'</a><div style="color: red; font-size: 9px;">'.$time.'&nbsp;ago</div></center></td><td width="5px"></td>';
$nb++;}

Use $nb to count the rows (note $nb++ to increment the $nb value). Then if $nb is a multiple of 7 (e.g. 7, 14, 21, etc.), then add </tr><tr>.

Not tested! At least, you know the way to proceed.
Give it a try and report back.

nfs2

10:12 am on Apr 5, 2006 (gmt 0)

10+ Year Member



That worked perfect! Thanks for the help :)