stig1 Posted March 2, 2009 Share Posted March 2, 2009 I have a database which i can pull out the required records. However, after every 3 records I would like to put in the following code <div class="clear"></div> So my layout will not look messy. How would I go about doing this? So if there is 13 records pulled out of the database, I should have 3 records, than a clear class and then another 3 records, then another clear class and so on... Link to comment https://forums.phpfreaks.com/topic/147499-quick-question-hopefully-simple-answer/ Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 $a = 0; while ($row = mysql_fetch_assoc($query)) { $a++; if ($a == 3) { echo "<div class='clear'></div>"; $a = 0; } // do your thing with the results } Link to comment https://forums.phpfreaks.com/topic/147499-quick-question-hopefully-simple-answer/#findComment-774274 Share on other sites More sharing options...
stig1 Posted March 2, 2009 Author Share Posted March 2, 2009 Will that script always put a clear in after each lot of 3 records? so like i have 7 records, i should get a clear after the 3rd record and the 6th record? Link to comment https://forums.phpfreaks.com/topic/147499-quick-question-hopefully-simple-answer/#findComment-774277 Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 it should =\ however i haven't tested it.. but its simple enough for me to say that it should lol Link to comment https://forums.phpfreaks.com/topic/147499-quick-question-hopefully-simple-answer/#findComment-774281 Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 sorry for double post.. it won't <?php $a = 0; while ($row = mysql_fetch_assoc($query)) { $a++; // do your thing with the results if ($a == 3) { echo "<div class='clear'></div>"; $a = 0; } } ?> now it should.. Link to comment https://forums.phpfreaks.com/topic/147499-quick-question-hopefully-simple-answer/#findComment-774284 Share on other sites More sharing options...
stig1 Posted March 2, 2009 Author Share Posted March 2, 2009 How can I get it to put a clear in after every 3 records that come out? Link to comment https://forums.phpfreaks.com/topic/147499-quick-question-hopefully-simple-answer/#findComment-774287 Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 ^^ that does it..? Link to comment https://forums.phpfreaks.com/topic/147499-quick-question-hopefully-simple-answer/#findComment-774297 Share on other sites More sharing options...
trq Posted March 2, 2009 Share Posted March 2, 2009 <?php $a = 0; while ($row = mysql_fetch_assoc($query)) { // do your thing with the results if ($a % 3) { echo "<div class='clear'></div>"; } $a++; } ?> Link to comment https://forums.phpfreaks.com/topic/147499-quick-question-hopefully-simple-answer/#findComment-774448 Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 <?php $a = 0; while ($row = mysql_fetch_assoc($query)) { // do your thing with the results if ($a % 3) { echo "<div class='clear'></div>"; } $a++; } ?> I was thinkin about usin a modulus but I don't really care much for redoing code and also.. $a % 3 would be true 2/3 times.. so you'd want to do something like: <?php $a = 0; while ($row = mysql_fetch_assoc($query)) { // do your thing with the results if (!($a % 3)) { echo "<div class='clear'></div>"; } $a++; } ?> Link to comment https://forums.phpfreaks.com/topic/147499-quick-question-hopefully-simple-answer/#findComment-774465 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.