Jump to content

Breaking A Loop To Insert Advertisement


AJayUK

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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  :shy:

Link to comment
Share on other sites

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++;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.