Page 1 of 1

Add rotator or creating banner in PHP

Posted: Sun May 24, 2009 4:25 pm
by baptiste2k8
Hi,

Just I want to create add rotator in my PHP. I have created one as bellow :

banner.php

Code: Select all

<center>
<?php
$Img1 = "Images/image1.jpg";
$Alt1 = "Jean Baptiste MINANI";
$Url1 = "http://www.chezjeanbaptiste.com";
 
$Img2 ="Images/image2.jpg";
$Alt2 = "We advertise for you";
$Url2 = "http://www.kist.ac.rw";
 
$Img3 ="Images/image3.jpg";
$Alt3 = "We advertise for you";
$Url3 = "http://www.kist.ac.rw";
 
$num = rand(1,3);
$Image = ${'Img'.$num};
$Alt = ${'Alt' .$num};
$URL = ${'Url'.$num};
echo '<a href="' . $URL . '"><img src="' . $Image . '" width="200" height="150" alt="'.$Alt.'"> </a>';
?> 
</center>
and I am calling it as :

Code: Select all

 
<div id="photo">
<?php include 'banner.php'; ?>
</div>

The problem is that when I load my page, I can see the first add selected from the list but it doen't refresh it( I mean it keeps that add only until when I refresh my page). When I hit refresh or reload button it picks another advert. now, How can make rotating through all the adds without hitting refreshing or using <META REFRESH > tag?


Thank you.

Re: Add rotator or creating banner in PHP

Posted: Sun May 24, 2009 5:03 pm
by menno_bieringa
Hi baptiste2k8,

2 things:

1. Yes, you need to use a refresh mechanism to have the banners rotate, but this will effect in the entire site reloading over and over again. Therefor some JavaScript (Ajax) or (uglier but easiest) add an iframe of the same size of your banners to the page that should contain the adds. That way you can have the banner reload automatically without reloading the page.

2. A nicer way to do this banner rotation thing is by adding the data to an array. That way it's cleaner and you can grab a random banner more easily.

See for example this untested piece of code:

Code: Select all

 
var $aAllBanners = array();
$aAllBanners[] = array('img'=>'Images/image1.jpg', 'alt'=>'Jean Baptiste MINANI', 'url'=>'http://www.chezjeanbaptiste.com');
$aAllBanners[] = array('img'=>'Images/image2.jpg', 'alt'=>'We advertise for you', 'url'=>'http://www.kist.ac.rw');
//etc...
 
$aBanner = array_rand($aAllBanners, 1);
 
echo '<a href="' . $aBanner['url'] . '"><img src="' . $aBanner['img'] . '" width="200" height="150" alt="'.$aBanner['alt'].'"> </a>';
 

Re: Add rotator or creating banner in PHP

Posted: Sun May 24, 2009 6:16 pm
by baptiste2k8
Thank you for your answer,

I don't want to change any thing to my banner, I am looking on how I it can rotate. you are suggesting me to use iframe, can you show me the example? just how to call my addrotator code and how to refresh it.

I appreciate your help.


Regards,

Re: Add rotator or creating banner in PHP

Posted: Sun May 24, 2009 8:51 pm
by mikemike
Instead of using <?php include banner.php ?> instead do:

Code: Select all

<iframe src="banner.php" width="WIDTH OF YOUR BANNER" height="HEIGHT OF YOUR BANNER" frameborder="0"></iframe>
Then inside your banner.php make sure you have the following meta tag:
<meta http-equiv="refresh" content="5" />

That will refresh the page every 5 seconds, change the value to whatever you wish.

But I agree with menno_bieringa, array are faster and easier to work with. AJAX would also be a better solution here.

Mike