fael097 Posted July 6, 2010 Share Posted July 6, 2010 hi, my question is simple, and i have no clue how to achieve that. i made a while(mysql fetch array) loop to put each database result on a <td>, but i want it to make a new <tr> each 4 tds, how to achieve that? thanks in advance Link to comment https://forums.phpfreaks.com/topic/206884-breaking-the-while-loop-each-x-entries/ Share on other sites More sharing options...
aeroswat Posted July 6, 2010 Share Posted July 6, 2010 $count = 0; while(condition) { $count++; if($count %4 == 0) { echo "</tr><tr>"; } } Link to comment https://forums.phpfreaks.com/topic/206884-breaking-the-while-loop-each-x-entries/#findComment-1081877 Share on other sites More sharing options...
fael097 Posted July 6, 2010 Author Share Posted July 6, 2010 exactly thanky Link to comment https://forums.phpfreaks.com/topic/206884-breaking-the-while-loop-each-x-entries/#findComment-1081880 Share on other sites More sharing options...
ChemicalBliss Posted July 6, 2010 Share Posted July 6, 2010 Wrote it now so here, You would use a counter, then modulus by 4 every loop - if the answer is 0 (no remainder) then insert a new tr row. echo("<table>"); // For loops are much more dynamic/customizable etc - for([executes before first loop];[condition to loop];[executes after each loop]) for($i=0, $row = mysql_fetch_array($result);$i<mysql_num_rows($result);$i++, $row = mysql_fetch_array($result)){ // ** This is your column data. $thisrow = "<td>test: ".$row['test']."</td>"; // If its the fourth row (also happens to execute when $i = 0, first row). if($i % 4 == 0){ // If its _not_ the very first row, end the <tr> tag. if($i != 0){ echo("</tr>\n"); } // Add a new <tr> tag echo("<tr>"); } // Echo this column(<td>) echo($thisrow); } echo("</table>"); Note- You can change the modulus to change the amount of columns you want per row. -cb- Link to comment https://forums.phpfreaks.com/topic/206884-breaking-the-while-loop-each-x-entries/#findComment-1081883 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.