dachshund Posted December 28, 2009 Share Posted December 28, 2009 Hi, at the moment on my site I have four ad banners, all the same size. At the moment they are in separate areas of the site and stay as they are. Now I would like to have them all in the same position, but on rotation/random. So the banner changes between all four each time you refresh/visit the page. I know how to do this for the image with rand() but not for the image and the accompanying like <a href=""> Anyone know how I can do this? Thanks! Link to comment https://forums.phpfreaks.com/topic/186506-ad-banners-set-to-random/ Share on other sites More sharing options...
Buddski Posted December 28, 2009 Share Posted December 28, 2009 Firstly I would create an array that holds the images src or filename and the associated url for the href.. then randomly grab an item from the array and display it.. Link to comment https://forums.phpfreaks.com/topic/186506-ad-banners-set-to-random/#findComment-984917 Share on other sites More sharing options...
mattal999 Posted December 28, 2009 Share Posted December 28, 2009 This would suit you nicely (UNTESTED): <?php $ads = array( array("http://www.google.com/", "/images/googlead.jpg"), array("http://www.yahoo.co.uk/", "/images/yahooad.jpg") ); $i = rand(0, (count($ads)-1)); echo "<a href=\"".$ads[$i][0]."\"><img src=\"".$ads[$i][1]."\" /></a>"; ?> You can then put that in a separate file and include it in a page. Link to comment https://forums.phpfreaks.com/topic/186506-ad-banners-set-to-random/#findComment-984927 Share on other sites More sharing options...
dachshund Posted December 28, 2009 Author Share Posted December 28, 2009 thanks that works great! Link to comment https://forums.phpfreaks.com/topic/186506-ad-banners-set-to-random/#findComment-984971 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.