Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Simple Banner Rotator

Simple Banner Rotator

PHP powered banner rotator using the power of flat files (txt files).

This week we will build a simple banner/image rotator. We will be doing this using flat files (txt files) and a couple of file functions in php.

Here is what the banner txt file should look like, you simply just put one image/link pair per line. The "[$]" seperator is used to split apart the img/link pairs. We use this interesting seperator to ensure it is not going to be contained in the link or image reference.

google.gif[$]http://www.google.com
mkeefedesign.gif[$]http://www.mkeefedesign.com
scriptplayground.gif[$]http://v2.scriptplayground.com

First off, define the variables for this project

//
// Customizable Settings for banner script
//
$banner_txt_file = 'my_banners.txt'; // Our txt file of banner references
$banner_links = true; // Show banner as just image or with link (href)

Now lets set up a function to contain all of our banner code and make it easier to call in the future.

function display_banner() {
	// use 'global' to reference our preset variables
	global $banner_txt_file, $banner_links
	
	mt_srand((double)microtime() * 100000);
	$banners = file($banner_txt_file);
	$count = count($banners);
	$random = rand(0, $count - 1);
	$banner = $banners[$random];
	$banner_parts = explode("[$]", $banner);
	$banner_img = $banner_parts[0];
	$banner_link = $banner_parts[1];

	$banner_html = "<img src=\"" . $banner_img . "\" />"; // build img tag
	if($banner_links) {
		$banner_html = "<a href=\"" . $banner_link . "\">" . $banner_html . "</a>";
	}
	return $banner_html; // return the html code
}

Now let's take a moment to explain what is happening in those last few lines. First off we define a few global variables to gain access to them in the function (scope issue).

Then we use mt_srand() to create a random seed, and ultimately allow us to pick a random banner image. Now we begin the file opening process and start reading in our banners and links. We then take these lines of link/img pairs and choose a random one to display. Finally we build our necessary html to display the banner and add the link info if $banner_links is set to true.

Note: Make sure you "chmod" the banner txt file, so it can actually be read. :)

To display a random banner, you simply place this PHP code somewhere in your file

print display_banner();

Thats it, you now have a working php banner script... next time we will create a feature packed version with MySQL, counters and all sorts of goodies.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Simple Banner Rotator

 thomas  Tue Jul 18, 2006 7:25 am  
maybe you should do a mysql database one if your looking for tutorial ideas, idk, just a suggestion
 Roxy  Mon Apr 16, 2007 1:36 pm  
I'm confused, how will it know what php file to read? do we put the link between the print display_banner ();???
 mkeefe  Mon Apr 16, 2007 1:48 pm  
This example is simply adding the image directly to this PHP page. However you could include this code and print it into another file or you could have this PHP file simply print out the image path, something like this:



The PHP would be modified to only return $banner_img and not the entire image string.
 Selva kumar  Thu Aug 23, 2007 1:29 am  
i used it very well and it works fine. but i want to display images which are get from database. how is possible to get the image paths from database to the banner.txt? i am using mysql database with php 5. Any helps really appreciated.
 Pino  Thu Jul 9, 2009 11:34 am  
Hello,

I am a newbie to php. I replicated your code and txt file but cannot get it to work. My txt file looks like this:
before_your_visit.jpg[$]http://localhost/essehealth/images
career_center.jpg[$]http://localhost/essehealth/images

I am trying to test from a development pc.

Any help would be great.

Pino
 mkeefe  Mon Jul 13, 2009 7:52 am  
@Pino - You may be loading the images from the wrong location, or the images may be misplaced as well. Do you receive any error messages?
 Ton ennemi  Fri Feb 5, 2010 9:51 pm  
Va dont chier crétin !
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles