henrikb Posted September 2, 2007 Share Posted September 2, 2007 Hi, I have searched for a solution but did not find it, sorry if answered already. In my database table I have the following 6 field values and need the banner/url's to rotate (no stats required): 1. banner image 1 2. banner url 1 3. banner image 2 4. banner url 2 5. banner image 3 6. banner url 3 So first view : banner image 1 with banner url 1 second view : banner image 2 with banner url 2 third view : banner image 3 with banner url 3 etc. etc. Probably simple just cannot figure it out. Thanks in advance, Henrik Quote Link to comment https://forums.phpfreaks.com/topic/67644-solved-how-do-i-use-rotation-newbie-question/ Share on other sites More sharing options...
Wuhtzu Posted September 2, 2007 Share Posted September 2, 2007 With PHP you can only get it to "rotate" when the user updates his or her browser. If you want them to rotate while the user is viewing a particular page you have to use java-script. What rotating pattern do you need? Just a random banner each time the user navigates to a new page or shift banner every 2 minutes? And do they have to be in order (1,2,3,4 not 4,5,2,1,3)? Quote Link to comment https://forums.phpfreaks.com/topic/67644-solved-how-do-i-use-rotation-newbie-question/#findComment-339799 Share on other sites More sharing options...
henrikb Posted September 2, 2007 Author Share Posted September 2, 2007 Hi, Sorry I was not clear, banner only shifts when page reloaded/new visitor - order 1,2,3,1,2,3,1,2,3 etc. hope that makes it clearer. Thanks Henrik Quote Link to comment https://forums.phpfreaks.com/topic/67644-solved-how-do-i-use-rotation-newbie-question/#findComment-339803 Share on other sites More sharing options...
Wuhtzu Posted September 2, 2007 Share Posted September 2, 2007 It does. The simple solution is this: <?php //Define an array containing pairs of banner images and banner urls $banners = array( array( 'banner' => 'banner1.jpg', 'url' => 'http://somesite.com' ), array( 'banner' => 'banner2.gif', 'url' => 'http://anothersite.com' ), array( 'banner' => 'banner3.jpg', 'url' => 'http://yetanothersite.com' ), array( 'banner' => 'banner4.png', 'url' => 'http://somesite.com' ) ); //Generate a random number between 0 and the number of elements in the banner array $rand = rand(0,count($banners)); //Use that random number to choose an element (image and url) from the array $banner_image = $banners[$rand]['banner']; $banner_url = $banners[$rand]['url']; ?> <!-- Display the banner --> <a href="<?php echo $banner_url;?>"><img src="<?php echo $banner_image;?>"></a> This is the simple version which chooses a random banner each time the browser is reloaded. To make it display the images in the right order gets more complex. Quote Link to comment https://forums.phpfreaks.com/topic/67644-solved-how-do-i-use-rotation-newbie-question/#findComment-339808 Share on other sites More sharing options...
henrikb Posted September 2, 2007 Author Share Posted September 2, 2007 Hi again, Should work fine, I appreciate you help! Thanks again, Henrik Quote Link to comment https://forums.phpfreaks.com/topic/67644-solved-how-do-i-use-rotation-newbie-question/#findComment-339812 Share on other sites More sharing options...
Wuhtzu Posted September 2, 2007 Share Posted September 2, 2007 <?php session_start(); //Define an array containing pairs of banner images and banner urls $banners = array( array( 'banner' => 'banner1.jpg', 'url' => 'http://somesite.com' ), array( 'banner' => 'banner2.gif', 'url' => 'http://anothersite.com' ), array( 'banner' => 'banner3.jpg', 'url' => 'http://yetanothersite.com' ), array( 'banner' => 'banner4.png', 'url' => 'http://somesite.com' ) ); if(isset($_SESSION['banner'])) { $num_of_banners = count($banners); if($_SESSION[banner] < $num_of_banners-1) { $banner = $_SESSION['banner'] + 1; } else { $banner = 0; } } else { $banner = 0; } $_SESSION['banner'] = $banner; $banner_image = $banners[$banner]['banner']; $banner_url = $banners[$banner]['url']; ?> [a href="<?php echo $banner_url; ?>"][img src="<?php echo $banner_image; ?>"][/a] This is the more "advanced" version. It has the order functionality. Try it at: http://wuhtzu.dk/random/banner_rotate.php Quote Link to comment https://forums.phpfreaks.com/topic/67644-solved-how-do-i-use-rotation-newbie-question/#findComment-339813 Share on other sites More sharing options...
henrikb Posted September 2, 2007 Author Share Posted September 2, 2007 Great thanks! Henrik Quote Link to comment https://forums.phpfreaks.com/topic/67644-solved-how-do-i-use-rotation-newbie-question/#findComment-339832 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.