PHP Code Share – Random this, random that

Sorry on the long delay between posts.  Been very busy at work, and sometimes haven’t had the brain energy to do even MORE web stuff in the off-hours.  But I’m definitely due so the next item is an easy one to work my way back into postings:  an easy to use random script.

Now I’ve seen many out there that claim to be simple, and then you start adding in files and iframes and that somehow doesn’t seem very simple in application.  So after trying a few things (a random link list for my home page, and a random image selector as well for the home page), here is what I settled on.

The Random Link script I use is one that I had actually found for use on another site.  It’s original purpose was to display random banners (with links) on a web page.  It worked very well and was very easy to adapt.  That was a few years ago.  I revisited the script while looking for others and realized it could be adapted easily to just display random links… or probably anything else.

Here is the original code:
<?php

//You will have to change the ban1, ban2, and ban3  variables.  You can change these links to point at the images you want displayed.
//You will have to change the url1, url2, and url3  variables.  You want these to be urls of the pages you want to link to.
$ban1 = “http://xxx.org/images/ads/banner.gif”;
$url1 = “http://www.xxx.org”;
$ban2 = “http://yyy.org/images/ads/banner.jpg”;
$url2 = “http://www.yyy.org”;
$ban3 = “http://zzz.org/images/ads/banner.gif”;
$url3 = “http://www.zzz.org”;
$target = “_blank”;

//Random number between 1 and number of banners.
$randomNum = rand (1,3);

//Select the banner of the random number
$image = ${‘ban’.$randomNum};

//Url of the random number
$url= ${‘url’.$randomNum};

//Prints out the link of the banner and the image of the banner
Print “<a href=”.$url.” mce_href=”.$url.” target=”.$target.”><img src=”.$image.” border=0></a>”;
?>

Pretty straightforward, huh?  So I took it and decided to use it just for random links:
<?php

$url1 = “http://www.google.com”;
$url2 = “http://www.yahoo.com”;
$url3 = “http://www.yocisco.com”;
$url4 = “http://www.rafaelnadal.com”;
$url5 = “http://www.wimbledon.org”;
$url6 = “http://www.odcdance.org”;
$url7 = “http://www.cuil.com”;
$url8 = “http://www.youtube.com”;
$url9 = “http://www.vrestaurant.com”;
$url10 = “http://www.atptennis.com”;
$url11 = “http://www.wheresgeorge.com”;
$url12 = “http://www.laughingsquid.net”;
$url13 = “http://www.blogger.com”;
$url14 = “http://www.meebo.com”;
$url15 = “http://www.usopen.org”;
$url16 = “http://www.campstella.org”;
$url17 = “http://www.youtube.com/watch?v=2keyIEof038”;
$target = “_blank”;

//Random number between 1 and number of banners.
$randomNum = rand (1,17);

//Select the banner of the random number
//$text = ${‘ban’.$randomNum};

//Url of the random number
$url= ${‘url’.$randomNum};

//TEST TEXT
$static1= “Random Link “;

//TEST TEXT
$static2= “… Give it a shot!”;

//Prints out the link of the banner and the image of the banner
Print “<a href=”.$url.” mce_href=”.$url.” target=”.$target.”><span>”.$static1.”</span><span><em>”.$static2.”</em></span></a>”;
?>

Isn’t simplicity a wonderful thing????? 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Verification *

This site uses Akismet to reduce spam. Learn how your comment data is processed.