Hi guys. I want to insert an advert after the first article on my blog. It's not a wordpress or other generic blog. I have an idea on how i should do it but it just doesn't work when i try. I use a do-while loop to call the articles. This is the code...

<?php do { ?>
<h2><a href="news.php?post_id=<?php echo $row_getPosts['post_id']; ?>"><?php echo $row_getPosts['title']; ?></a></h2>
<p class="updated"><?php echo $row_getPosts['formatted']; ?> | By <a href="#"><?php echo $row_getPosts['name']; ?></a></p>
<p><?php echo nl2br($row_getPosts['blog_entry']); ?></p>
<?php } while ($row_getPosts = mysql_fetch_assoc($getPosts)); ?>

Please help...

Recommended Answers

All 4 Replies

Sounds reasonable enough.

<?php

$advert = true;

do {
    // Output your article HTML

    if($advert) {
        // Output your advert
    }

    $advert = false;
} while($row_getPosts = mysql_fetch_assoc($getPosts));
 do {
     // Output your article HTML
     if($advert) {
     // Output your advert
 }

I don't want to confuse mtho. But I would like to understand this section of code.

OK. It is going to start printing out the first article and then it will check for the boolean value of $advert. Since the value is set to true, it will display an ad and set the value of $advert to false. Won't that happen to the second, third and fourth articles and so on?

No, because the variable $advert is set to true outside and before the loop and then set to false during each iteration of the loop. Therefore it will be displayed once.

It worked a treat. Thanks blocblue, you solved it for us mate

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.