nita Posted May 5, 2007 Share Posted May 5, 2007 Hi. I would like to display results from database in a table split in 4 columns and rows depending on number of the items to display. (in this case - dvd covers) [1 row x 4 images(columns) ] Code submitted is displaying them all in 1 row, <? include "connectdb.php"; echo " <table> <tr>"; $result = mysql_query("SELECT * FROM movies WHERE cat='Adventure'") or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo " <td> <img src='http://www.nita-on-line.com/knmc/covers/"; echo $row['cover']; echo ".jpg' border='0'>"; echo "</td>"; } echo "</tr> </table> "; ?> how can i do that ??? thank you for your advice in advance ! nita Link to comment https://forums.phpfreaks.com/topic/50121-solved-php-mysql-diplay-results-in-table-with-4-columns/ Share on other sites More sharing options...
Barand Posted May 5, 2007 Share Posted May 5, 2007 Example <?php include 'db.php'; define ("NUMCOLS",4); $res = mysql_query("SELECT col1, col2 FROM mytable"); $count = 0; echo "<TABLE border=1>"; while (list($col1, $col2) = mysql_fetch_row($res)) { if ($count % NUMCOLS == 0) echo "<TR>\n"; # new row echo "<TD>$col1<br>$col2</TD>\n"; $count++; if ($count % NUMCOLS == 0) echo "</TR>\n"; # end row } # end row if not already ended if ($count % NUMCOLS != 0) { while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</TR>\n"; } echo "</TABLE>"; ?> Link to comment https://forums.phpfreaks.com/topic/50121-solved-php-mysql-diplay-results-in-table-with-4-columns/#findComment-246097 Share on other sites More sharing options...
nita Posted May 5, 2007 Author Share Posted May 5, 2007 Thank You Barand. ! This is a solution i was looking for. It's also very educational for me as a beginner in php. nita. Link to comment https://forums.phpfreaks.com/topic/50121-solved-php-mysql-diplay-results-in-table-with-4-columns/#findComment-246111 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.