Add rotator or creating banner in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
baptiste2k8
Forum Newbie
Posts: 4
Joined: Sun May 24, 2009 4:18 pm

Add rotator or creating banner in PHP

Post 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.
Last edited by Benjamin on Tue May 26, 2009 10:29 am, edited 1 time in total.
Reason: Added [code=php] tags.
menno_bieringa
Forum Newbie
Posts: 1
Joined: Sun May 24, 2009 4:11 pm
Location: Argentina

Re: Add rotator or creating banner in PHP

Post 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>';
 
Last edited by Benjamin on Tue May 26, 2009 10:30 am, edited 1 time in total.
Reason: Changed code type from text to php.
baptiste2k8
Forum Newbie
Posts: 4
Joined: Sun May 24, 2009 4:18 pm

Re: Add rotator or creating banner in PHP

Post 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,
Last edited by Benjamin on Tue May 26, 2009 10:30 am, edited 1 time in total.
Reason: Removed [quote] of entire op.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Add rotator or creating banner in PHP

Post 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
Last edited by Benjamin on Tue May 26, 2009 10:30 am, edited 1 time in total.
Reason: Changed code type from text to html.
Post Reply