Reef Posted April 29, 2008 Share Posted April 29, 2008 Hi, I'm a noob (so I'm expecting to be shot). I've just started to learn PHP by myself however I am asking for help with something I need to get done asap really. I want to set a rotating ad-banner so that when its on the site, it changes the image and link on every page load, upto 10 different images/links Can anyone point me in the right direction ? I would REALLY appreciate it oh and if this is the wrong section - sorry. Link to comment https://forums.phpfreaks.com/topic/103488-rotating-ad-banner-system/ Share on other sites More sharing options...
Fadion Posted April 30, 2008 Share Posted April 30, 2008 For a random approach: <?php $banners = array('yahoo', 'google', 'live'); $rand = rand(0, count($banners)-1); echo "<a href='http://www." . $banners[$rand] . ".com'><img src='banners/" . $banners[$rand] . ".jpg' /></a>"; ?> The $banners array holds the banners names, assuming u name your image files as a the sites link, so u spare some time and code (dont go on a lot of if statements). For a real rotator, meaning that u need to go from banner1 to banner10 incrementaly each page reload, consider the following script: <?php session_start(); $banners = array('yahoo', 'google', 'live'); if(isset($_SESSION['position']) and $_SESSION['position'] != count($banners)-1){ $_SESSION['position']++; } else{ $_SESSION['position'] = 0; } $bannerKey = $_SESSION['position']; echo "<a href='http://www." . $banners[$bannerKey] . ".com'><img src='banners/" . $banners[$bannerKey] . ".jpg' /></a>"; ?> It uses sessions to keep track of the actual banner and increments it each reload. Like the first example, this uses an array for banner names. Feel free to try and modify it and ask for further help. Link to comment https://forums.phpfreaks.com/topic/103488-rotating-ad-banner-system/#findComment-529951 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.