PaulBiff Posted March 26, 2008 Share Posted March 26, 2008 Hey all, I am pretty new to PHP, as you will probably tell from this question. What I am trying to do is grab data from 2 columns from a table in a mySQL database and then display it in a grid as follows: product1name<br>product1link product2name<br>product2link product3name<br>product3link product4name<br>product4link product5name<br>product5link product6name<br>product6link product7name<br>product7linketcetc..[/td] where product1name and product1link etc is the data that is pulled from the database and inserted into the same table cell. The page will change depending on what product category is selected which means the grid can have 2 to 24 products called. I have no problems getting the data from the database and displayed on a webpage in a simple 1 column table; I just can't work out how to make it display in the grid format, with a maximum of 6 columns. Thanks for any pointers, Paul Quote Link to comment https://forums.phpfreaks.com/topic/98018-displaying-data-in-a-grid/ Share on other sites More sharing options...
MadTechie Posted March 26, 2008 Share Posted March 26, 2008 see example here http://www.phpfreaks.com/forums/index.php/topic,188922.msg847244.html#msg847244 UPDATE: try something like this <?php echo "<table border=\"0\">\n<tr>\n"; $c = 4; //Rows $t=0; while ($row = mysql_fetch_assoc($result)) { $t++; if(!($t % $c)) echo "</tr><tr>"; echo "<td>"; echo $row["userid"]; echo $row["fullname"]; echo "</td>"; } if(!($t % $c)) echo "</tr>"; echo "</table>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98018-displaying-data-in-a-grid/#findComment-501518 Share on other sites More sharing options...
soycharliente Posted March 26, 2008 Share Posted March 26, 2008 Just want to make the comment that I don't think that code accounts for leaving the while loop early. Like if you fill up 4 boxes on the first row and then only 1 on the second, your table will be messed up since there won't be 3 more empty cells to fill the row in completely. Not tested, but just wrote it to show theory. <?php echo "<table><tr>"; $cells = 4; // Cells per row $total = 0; $early = 0; while ($row = mysql_fetch_array($result)) { $t++; $mod = $total % $cells; if (!$mod) { echo "</tr><tr>"; } echo "<td>{$row['userid']} <br /> {$row['fullname']}</td>"; $early = $cells - $mod; } if(!$mod) { echo "</tr>"; $early = 0; } if ($early) { for ($i = 1; $i <= $early; $i++) { echo "<td> </td>"; } echo "</tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98018-displaying-data-in-a-grid/#findComment-501536 Share on other sites More sharing options...
revraz Posted March 26, 2008 Share Posted March 26, 2008 BTW, the "grid" is called a HTML Table. Quote Link to comment https://forums.phpfreaks.com/topic/98018-displaying-data-in-a-grid/#findComment-501543 Share on other sites More sharing options...
PaulBiff Posted March 27, 2008 Author Share Posted March 27, 2008 Thanks to all for your help! I got the table/grid working correctly now and learnt a bit more PHP along the way! Paul Quote Link to comment https://forums.phpfreaks.com/topic/98018-displaying-data-in-a-grid/#findComment-502007 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.