MstrGmrDLP Posted April 11, 2014 Share Posted April 11, 2014 Okay, I am having a little trouble with a mysql table. I am trying to make a table. I have a <tr> in a while statement. I am trying to make it so that it will go and display 5 users across the <tr> and then it will close the tr and start a new one and repeat showing the next 5 users. What would the code for this be? Any help would be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/287687-mysql-help/ Share on other sites More sharing options...
requinix Posted April 11, 2014 Share Posted April 11, 2014 Keep a counter that starts at 1 and increments for every table cell you output. At the beginning of the loop, if the value is 1 then output a . At the end of the loop, when the (new) value is 5 then output a and reset to 1, otherwise just increment. After the loop be sure to clean up any incomplete row you may be in the middle of outputting: if the counter is >1 then output however many cells it takes to get the counter back up to 5 (keeping in mind that counter=2 means you need four cells, not three). Quote Link to comment https://forums.phpfreaks.com/topic/287687-mysql-help/#findComment-1475736 Share on other sites More sharing options...
Solution Psycho Posted April 11, 2014 Solution Share Posted April 11, 2014 I prefer to let the counter just increment by one on each iteration and not reset it. Instead, you can just use the modulus operator. Example: $columns = 5; $count = 0; while($row = mysql_fetch_assoc($result)) { $count++; //Open new row if needed if($count%$columns == 1) { echo "<tr>\n"; } //Display the record echo "<td>"; //Record output goes here echo "</td>"; //Close the row if needed if($count%$columns == 0) { echo "</tr>\n"; } } //After loop, close row if it didn't end with full last row if($count%$columns != 0) { echo "</tr>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/287687-mysql-help/#findComment-1475744 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.