blog
HOME · CREATIVE · WEB · TECH · BLOG

Thursday, November 6th, 2008

Dynamic Content With PHP Arrays (No Databases)

You don't actually need to have a database to have some dynamic content on your site. You can use arrays instead. Let's say you want to randomly show 3 Star Wars DVD box covers from Amazon.com with links to the item on Amazon. To do that you'd need three things - 1) The title of the movie, 2) the URL of the box cover, and 3) the link to the item.

You define the values that you'll choose from in PHP arrays. To do that you do something like this...

<?php
$titles = array ("Star Wars - Episode I, The Phantom Menace", "Star Wars - Episode II, Attack of the Clones", "Star Wars - Episode III, Revenge of the Sith", "Star Wars Episode IV - A New Hope", "Star Wars Episode V - The Empire Strikes Back", "Star Wars Episode VI - Return of the Jedi", "Star Wars - Clone Wars, Vol. 1", "Star Wars - Clone Wars, Vol. 2");

$pageURLs = array("http://www.amazon.com/Star-Wars-Episode-Phantom-Widescreen/dp/B00003CX5P/ref=sr_1_4?ie=UTF8&s=dvd&qid=1225998531&sr=1-4", "http://www.amazon.com/Star-Wars-Episode-Attack-Widescreen/dp/B00006HBUJ/ref=sr_1_7?ie=UTF8&s=dvd&qid=1225998531&sr=1-7", "http://www.amazon.com/Star-Wars-Episode-Revenge-Widescreen/dp/B00005JLXH/ref=sr_1_6?ie=UTF8&s=dvd&qid=1225998531&sr=1-6", "http://www.amazon.com/Star-Wars-Episode-IV-Widescreen/dp/B000FQJAIW/ref=sr_1_5?ie=UTF8&s=dvd&qid=1225998531&sr=1-5", "http://www.amazon.com/Star-Wars-Episode-Versions-Widescreen/dp/B000FQJAJG/ref=sr_1_9?ie=UTF8&s=dvd&qid=1225998531&sr=1-9", "http://www.amazon.com/Star-Wars-Episode-VI-Widescreen/dp/B000FQVX78/ref=sr_1_10?ie=UTF8&s=dvd&qid=1225998531&sr=1-10", "http://www.amazon.com/Star-Wars-Clone-Vol-1/dp/B0006Z2LMO/ref=sr_1_12?ie=UTF8&s=dvd&qid=1225998531&sr=1-12", "http://www.amazon.com/Star-Wars-Clone-Vol-2/dp/B000BCE8Q4/ref=sr_1_14?ie=UTF8&s=dvd&qid=1225999439&sr=1-14");

$imgURLs = array ("http://ecx.images-amazon.com/images/I/51BR3RNVKZL._SL160_AA115_.jpg", "http://ecx.images-amazon.com/images/I/51BGV8AJ4RL._SL160_AA115_.jpg", "http://ecx.images-amazon.com/images/I/51RHXMVH9YL._SL160_AA115_.jpg", "http://ecx.images-amazon.com/images/I/517M4V93K0L._SL160_AA115_.jpg", "http://ecx.images-amazon.com/images/I/510GTNVNYYL._SL160_AA115_.jpg", "http://ecx.images-amazon.com/images/I/51CWJ6PC1KL._SL160_AA115_.jpg", "http://ecx.images-amazon.com/images/I/51A6GEB6SAL._SL160_AA115_.jpg", "http://ecx.images-amazon.com/images/I/51F7C82EDGL._SL160_AA115_.jpg");

$titleCount = count($titles);
$element1 = rand(0, $titleCount-1);
$element2 = rand(0, $titleCount-1);
if ($element2==$element1) {$element2 = rand(0, $titleCount-1);}
$element3 = rand(0, $titleCount-1);
if ($element3==$element1) {$element3 = rand(0, $titleCount-1);}
if ($element3==$element2) {$element3 = rand(0, $titleCount-1);}
?>

So you'll need something like that to initialize the arrays. Then you use them by displaying an image with a link to the page on Amazon.com. The code for the image and link would look something like this..

<a href="<?php echo $pageURLs[$element1]; ?>" target="amazon"><img src="<?php echo $imgURLs[$element1]; ?>" alt="<?php echo $titles[$element1]; ?>" title="<?php echo $titles[$element1]; ?>" /></a>

So the basic syntax is a PHP echo that specifies the array and the value of the element of the array. $titles[$element1] means the $title array and the element of the array is the value found in the $element1 variable. If you had put $titles[1] that would be element 1 of the array (which is actually the second element in the array since elements start their counting at zero, not 1).

So let's see it in action...

Star Wars Episode V - The Empire Strikes Back   Star Wars - Clone Wars, Vol. 2   Star Wars Episode IV - A New Hope

Now, if you take it a step further, the definition of the arrays can happen in an external file and put into the page with a PHP include. That file can be written with a database application. Which means you could have a site with dynamic content running without any databases on the web server itself.

There are many uses for this type of thing - imagine a "picture of the day" script, or a banner ad rotating script. The options are pretty wide ranging.

Tags:
Categories: PHP

Leave a Reply

HOME · CREATIVE · WEB · TECH · BLOG