robcrozier Posted March 1, 2007 Share Posted March 1, 2007 hi all, what i'm trying to do is display some output from a database in a table until there are no more records left. The data being displayed is actually a link to an image, so images are therefore outputted in the table. I can quite easily get the table to display one '<tr><td>$content</td></tr>' above another until there are no more records, however i would like to display 3 pictures in a row before returning to the next. so for example: <tr><td>$content</td><td>$content</td><td>$content</td></tr> repeat until no more data.... However... i just keep getting a pile of garbage. it outputs far too many rows! Please help! Quote Link to comment https://forums.phpfreaks.com/topic/40708-nicely-displaying-output-from-database/ Share on other sites More sharing options...
trq Posted March 1, 2007 Share Posted March 1, 2007 There is a nice thread explaining this in the FAQ / Snippets board. Quote Link to comment https://forums.phpfreaks.com/topic/40708-nicely-displaying-output-from-database/#findComment-197024 Share on other sites More sharing options...
Snooble Posted March 1, 2007 Share Posted March 1, 2007 <?php $query = "SELECT * FROM table"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); echo "<br><br><b>Let's fetch data...</b><br>"; echo "<table border='1' width='98%'>\n"; echo "<tr> <th>HEADER1</th> <th>HEADER2</th> <th>HEADER3</th> <th>HEADER4</th> </tr>"; $count = 0; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } $count++; echo "\t</tr>\n"; } echo "</table>\n"; if ($count < 1) { echo "<br><br>No rows were found in this table.<br><br>"; } else { echo "<br><br>".$count; echo " rows were found in this table.<br><br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/40708-nicely-displaying-output-from-database/#findComment-197026 Share on other sites More sharing options...
robcrozier Posted March 1, 2007 Author Share Posted March 1, 2007 Hi, just tried that and all i get is 4 column headers and every entry from database displayed on individual row beneath 1st two columns??? Quote Link to comment https://forums.phpfreaks.com/topic/40708-nicely-displaying-output-from-database/#findComment-197043 Share on other sites More sharing options...
simcoweb Posted March 1, 2007 Share Posted March 1, 2007 You could display them like this. Set the column variable to whatever you want: <?php // display results $totalColumns = 3; $i = 1; echo "<table border='0' cellpadding='2'>"; while ($row = mysql_fetch_array($results)){ if ($i == 1) echo "<tr>"; echo "<td>"; echo "your_image_reference_here"; echo "</td>"; if ($i == $totalColumns) { echo "</tr>"; $i = 0; } $i++; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/40708-nicely-displaying-output-from-database/#findComment-197060 Share on other sites More sharing options...
robcrozier Posted March 1, 2007 Author Share Posted March 1, 2007 sorted, faq snippets worked! Quote Link to comment https://forums.phpfreaks.com/topic/40708-nicely-displaying-output-from-database/#findComment-197063 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.