Jump to content

Recommended Posts

I have this Banner ad rotation script.

$banner[1] = '<a href="http://www.facebook.com/mstrgmrs"><img src="/adverts/mstr_gmrs.png" width="900" height="200" border="1"></a>';
$banner[2] = '<a href="http://www.twitter.com/mstrgmrs"><img src="/adverts/banner_ad.png" width="900" height="200" border="1"></a>';
$banner[3] = '<a href="http://www.youtube.com/user/mstrgmrs"><img src="/adverts/banner_ad.png" width="900" height="200" border="1"></a>';
$banner[4] = '<a href="http://mastergmrs.enjin.com/"><img src="/adverts/banner_ad.png" width="900" height="200" border="1"></a>';
$id = rand(1,4);
echo $banner[$id];

How can I make it so that code can work with a database?

Link to comment
https://forums.phpfreaks.com/topic/287702-banner-ad-rotation/
Share on other sites

If you're trying to get the images to rotate without the page reloading, you'll need to use a client-side scripting language like JavaScript.

https://www.google.com/search?q=javascript+rotating+images

 

You can still use PHP to get the image information, you'll just need to load it into JavaScript.

Edited by cyberRobot
Link to comment
https://forums.phpfreaks.com/topic/287702-banner-ad-rotation/#findComment-1475793
Share on other sites

To make it database driven you simply need to create a banner table.

 

You'll want to have a structure like this:

 

id int (primary key)
url varchar(512)
image varchar(128)
active tinyint
As for rotation, so long as your banner ad table doesn't get larger than a thousand rows, there isn't much of a concern, however, I would highly recommend reading this blog post: http://jan.kneschke.de/projects/mysql/order-by-rand/

 

With that said, the simplest solution for you, that will certainly work fine for quite a while:

 

SELECT * FROM banner WHERE active = 1 ORDER BY RAND() LIMIT 1
If you expect rotation over time without reloads, then cyberRobot's point should be taken into account. Often people will utilize an iframe and place the banner in the iframe, which can then have a simple meta refresh tag in the header of the code.

 

Ajax polling offers a more sophisticated option to cyberRobot's point, or you could alternatively fetch a number of ads initially and use a javascript timer to rotate them.

Link to comment
https://forums.phpfreaks.com/topic/287702-banner-ad-rotation/#findComment-1475800
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.