Question

I'm creating my website located here

I'm trying to add a banner that changes every time a page refreshes. I have set up 2 examples in my database called "link 1" and "link 2". I will want to add more as and when I get them.

What I want to happen is this:

I want to display one of the 2 images on my site and when the user refreshes the page, it will select one of the 2 images and this should continue every time the page refreshes.

I'm testing this out in a page called banner.php before I move it to my footer.php and make it live.

I currently have this code In my banner.php page:

<?PHP
include_once('include/connection.php');

// Edit this number to however many links you want displaying
$num_displayed = 1 ;

// Select random rows from the database
    global $pdo;
      $query = $pdo->prepare ("SELECT * FROM banners ORDER BY RAND() LIMIT $num_displayed"); 
      $query->execute();

// For all the rows that you selected
while ($row = execute($result)) 

{
// Display them to the screen...

echo "<a href=\"" . $row["link"] . "\">
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>" ;
}
?>
<br /><br /><br />

But I am getting this error code:

Fatal error: Call to undefined function execute() in banner.php on line 13

My connection page is used by other pages so I know it works.

Please can some one help me on what I am doing wrong.

Need any more info then please ask and I will add it to this post.

Thank you.

Kev

Was it helpful?

Solution

replace this

while ($row = execute($result)) 

with this:

while ($row = $query->fetch())

EDIT

This make it better to read.

while ($row = $query->fetch()) :
// Display them to the screen...
?>
<a href="<?php echo $row['link']; ?>">
<img src="<?php echo $row['image']; ?>" border="0" alt="<?php echo $row['text'];?>">
</a>
<?php endwhile; ?>
<br/>
<br/>
<br/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top