Jump to content

[SOLVED] How do I use rotation? newbie question


Recommended Posts

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

 

 

 

 

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)?

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.

 

<?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

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.