MstrGmrDLP Posted April 11, 2014 Share Posted April 11, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/287702-banner-ad-rotation/ Share on other sites More sharing options...
MstrGmrDLP Posted April 11, 2014 Author Share Posted April 11, 2014 Sorry if this is simple. I over think stuff a lot and I can't figure it out because I think to hard about it. Quote Link to comment https://forums.phpfreaks.com/topic/287702-banner-ad-rotation/#findComment-1475791 Share on other sites More sharing options...
cyberRobot Posted April 11, 2014 Share Posted April 11, 2014 (edited) 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 April 11, 2014 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/287702-banner-ad-rotation/#findComment-1475793 Share on other sites More sharing options...
gizmola Posted April 11, 2014 Share Posted April 11, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287702-banner-ad-rotation/#findComment-1475800 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.