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... Quote 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 } Quote 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? Quote 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 Quote 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.. Quote 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? Quote 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..? Quote 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++; } ?> Quote 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++; } ?> Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.