Building an advertising system with PHP, Part 1
(Page 2 out of 3)Creating the ad system
Our ad system will pull it's advertisements from an array that looks like this:
$ads = array(
// Array (link to website, image url)
array('http://phpit.net', '../phpitbanner.gif'),
array('http://www.aspit.net', '../aspitbanner.gif'),
array('http://www.google.com', '../468x60banner.gif'),
);
As you can see it's very easy to understand, and all we need is two things: a link and an image url.
What our ad system must do now is pull a random ad from the above array, and display it using JavaScript. The code to do this looks something like this:
// ### Advertisements:
$ads = array(
// Array (link to website, image url)
array('http://phpit.net', '../phpitbanner.gif'),
array('http://www.aspit.net', '../aspitbanner.gif'),
array('http://www.google.com', '../468x60banner.gif'),
);
// Get a random ad
$adkey = array_rand($ads, 1);
// Get ad
$ad = $ads[$adkey];
$url = $ad['0'];
$image = $ad['1'];
// Display ad:
header ("Content-Type: text/javascript");
echo "document.write ('\"$url\">\"
?>
If you now run the above code in conjunction with the ad page, which looks like this:
You will notice that random ads are displayed. We've now got a very basic, but functional advertising system, using nothing more than some simple PHP and JavaScript.
December 5th, 2005 at 12:14 am
[…] var site=”s20phpit” « Previous Building an advertising system with PHP, Part 1 […]
December 18th, 2005 at 9:19 pm
[…] Welcome to part 3 of the “Building an advertising system with PHP” series. In the previous parts (part 1 and part 2) I have shown you how to build your own advertising system using PHP and JavaScript. We’ve also added two extra features to our ad system and in part 2 we built a page to manage the ads as well. If you haven’t read either part yet, I highly recommend doing so before reading this part. […]
December 19th, 2005 at 9:33 pm
[…] PHPit.net’te �ok g�zel bir reklam (banner) y�netim sistemi olu�turmak i�in 3 par�al�k bir makale serisi (1., 2., 3. b�l�m)yazm��lar. Kesinlikle �neririm, veritaban�, javascript vs. yi kullan��lar� ger�ekten bence hem k�sa hem efektif olmu� diyebilirim. �ok be�endim. […]
June 24th, 2006 at 11:34 pm
[…] Stage1 […]