0

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...

3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by mtho
1

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));
0
 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?

1

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.