AJayUK Posted June 30, 2010 Share Posted June 30, 2010 Hello everybody, Ive got a problem with some PHP, I usually figure stuff out for myself but unfortunately im a little stuck on this one even though its probably easy... easy if you know how to do it I guess. I have a loop displaying news articles down my page... however I would like to break the loop after every 4 entries so that I can add an advertisement banner... then the loop to begin from where it started. Below is an example and ive used numbers for the news articles just so its more clear... 1 2 3 4 *Advertisement* 5 6 7 8 *Advertisement* 9 10 11 ... and so on So im just needing help on how i can add insert an advertisement banner after every 4 entries in a loop but for the loop to carry on as normal after the advertisement banner has been placed. Any help would be very appreciated. AJay Quote Link to comment https://forums.phpfreaks.com/topic/206277-breaking-a-loop-to-insert-advertisement/ Share on other sites More sharing options...
magnetica Posted June 30, 2010 Share Posted June 30, 2010 Hi When in the loop check if the number is divisible by 5. if ( ( $num % 5 ) == 0){ // check that number divided by 5 leaves no remainder print "Number is divisible by 5"; //show advert } Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/206277-breaking-a-loop-to-insert-advertisement/#findComment-1079130 Share on other sites More sharing options...
AJayUK Posted June 30, 2010 Author Share Posted June 30, 2010 Hmm im not sure what $num is supposed to equal? If $num is supposed to equal the number of rows in the loop, i tried that with your code but it didnt work. Quote Link to comment https://forums.phpfreaks.com/topic/206277-breaking-a-loop-to-insert-advertisement/#findComment-1079132 Share on other sites More sharing options...
magnetica Posted June 30, 2010 Share Posted June 30, 2010 Hi $num would be the iteration number of the loop, as to check your on a multiple of 5 in order to show your advert. Quote Link to comment https://forums.phpfreaks.com/topic/206277-breaking-a-loop-to-insert-advertisement/#findComment-1079135 Share on other sites More sharing options...
Adam Posted June 30, 2010 Share Posted June 30, 2010 Hi When in the loop check if the number is divisible by 5. if ( ( $num % 5 ) == 0){ // check that number divided by 5 leaves no remainder print "Number is divisible by 5"; //show advert } Hope this helps Unfortunately that type of approach won't work. Have you tried running a simple example? Depending on the way you implement it, the first advert would appear in-place on the first run, but after that the order would become messed up. The only way for it to remain after every 4 using that method would be to display the advert and continue (skip) to the next iteration; which obviously you don't want to do. If you displayed both the advert and the current iteration, you'd be inserting an extra row and so the advert would appear after the 4th on the first appearance, but every 5th after that. --- What you need to do is increment a counter, and as it reaches 4 reset it. Rough example to demonstrate what I mean: $ad = 0; for ($i = 1; $i <= 20; $i++) { if ($ad == 4) { echo 'advertisement<br />'; $ad = 0; } echo $i . '<br />'; $ad++; } Not tested that code. Quote Link to comment https://forums.phpfreaks.com/topic/206277-breaking-a-loop-to-insert-advertisement/#findComment-1079137 Share on other sites More sharing options...
AJayUK Posted June 30, 2010 Author Share Posted June 30, 2010 Hi MrAdam, That approach worked however the problem is that the counter seems to rely on numbers. When i posted the example, I used numbers only to show the continuation after the advertisement. However im needing to post news articles and not numbers (sorry i should have made my post more clear). So im trying to show 4 news articles, then an advertisement, the next 4 news articles then an advertisement. Im sorry, im no php expert yet and i know people get frustrated with 'noobs' so i dont want to stress anyone out, i think this is a job for getafreelancer Quote Link to comment https://forums.phpfreaks.com/topic/206277-breaking-a-loop-to-insert-advertisement/#findComment-1079139 Share on other sites More sharing options...
Adam Posted June 30, 2010 Share Posted June 30, 2010 That was just a rough example, it can be adapted easily. How exactly are you looping? Quote Link to comment https://forums.phpfreaks.com/topic/206277-breaking-a-loop-to-insert-advertisement/#findComment-1079141 Share on other sites More sharing options...
AJayUK Posted June 30, 2010 Author Share Posted June 30, 2010 Ok well heres my statement incase its needed SELECT * FROM news order by postdate desc and heres my code showing the news articles while ($row = mysql_fetch_assoc ($result)) { $date = $row['postdate']; $date = date("D dS M", strtotime($date) ); $title = stripslashes ($row['title']); $image = stripslashes (strip_tags($row['image1'])); echo "<div class='entry'> <div class='image'><img src='images/news/$image' width='108' height='78'></div> <div><a href='article.php?id=$row[id]'>$title</a></div> </div>"; } id like the advertisement to come after the above code but if i add the advertisement, it will post after every news article, instead i just want the advertisement to post after every 4 news articles. Like i said before though, I dont want to stress anyone out over this Quote Link to comment https://forums.phpfreaks.com/topic/206277-breaking-a-loop-to-insert-advertisement/#findComment-1079146 Share on other sites More sharing options...
Adam Posted June 30, 2010 Share Posted June 30, 2010 No worries.. $ad = 0; while ($row = mysql_fetch_assoc ($result)) { if ($ad == 4) { //display advert $ad = 0; } $date = $row['postdate']; $date = date("D dS M", strtotime($date) ); $title = stripslashes ($row['title']); $image = stripslashes (strip_tags($row['image1'])); echo "<div class='entry'> <div class='image'><img src='images/news/$image' width='108' height='78'></div> <div><a href='article.php?id=$row[id]'>$title</a></div> </div>"; $ad++; } Quote Link to comment https://forums.phpfreaks.com/topic/206277-breaking-a-loop-to-insert-advertisement/#findComment-1079168 Share on other sites More sharing options...
AJayUK Posted June 30, 2010 Author Share Posted June 30, 2010 Thanks MrAdam, that worked a treat! I cant thank you enough I guess when you have the code and look at it, its pretty simple Thanks again man Quote Link to comment https://forums.phpfreaks.com/topic/206277-breaking-a-loop-to-insert-advertisement/#findComment-1079177 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.