Creating a PHP Random Ad Script
After many tutorials have been with randomosity as their key point, why not one
that could actually be useful?
In this tutorial I will show you the steps frm beginning to end (with explination) on how
to create your own random ad manager.
First of we will start with most obvious starting point- The opening PHP tag
<?
Next we will define what are ads are and their location
$ads = array("http://site.com/image1.gif", "http://site.com/image2.gif", "http://site.com/image3.gif"); ///Since their are multiple ads we will use the array function
Next we will define the links that will go with each of the ads. REMEMBER the links must be in the same order as their ad images!
$links = array("http://link1.com", "http://link2.com", "http://link3.com");
Now to the harder stuff, we must take a direct count of the number of ads, so we will use the count function
$total_ads = count($ads);
And now we must have the ads randomly picked
$pick_ad = rand(0, $total_ads-1);
Finally we must echo our results to be displayed on your page
echo '<img src="' . $ads[$pick_ad] . '">';
Followed by the closing PHP tag
?>
The whole script combined-
<?
$ads = array("http://site.com/image1.gif", "http://site.com/image2.gif", "http://site.com/image3.gif");
$links = array("http://link1.com", "http://link2.com", "http://link3.com");
$total_ads = count($ads);
$pick_ad = rand(0, $total_ads-1);
echo '<img src="' . $ads[$pick_ad] . '">';
?>
This script is rather short and simple but is very nice when you have affiliates you want to show.
This tutorial is meant for PHP: Main and was brought to you by Jams44