Topic awaiting preservation: Help convert JS to PHP (Page 1 of 1) |
|
---|---|
Paranoid (IV) Inmate From: |
posted 12-17-2008 01:26
I'm currently using the script below to bring a visitor to a page at random when clicking 1 link. The pages are contained in an array. code: <script language="JavaScript" type="text/javascript"> var randomlinks=new Array() randomlinks[0]="http://www.sleepingwolves.com/blog?page_id=53" randomlinks[1]="http://www.sleepingwolves.com/blog?page_id=133" more links here..... ........... ............ function randomlink(){ window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)] } //--> </script>
code: <div id="cont" align="center"><a href="javascript:randomlink()">
|
Paranoid (IV) Inmate From: |
posted 12-17-2008 02:37
Never mind. Found a script and modified it. code: <li><? $linkarray = array("http://www.sleepingwolves.com/blog?page_id=53", "http://www.sleepingwolves.com/blog?page_id=134", "http://www.sleepingwolves.com/blog?page_id=133", "http://www.sleepingwolves.com/blog?page_id=133", "http://www.sleepingwolves.com/blog?page_id=151", "http://www.sleepingwolves.com/blog?page_id=53", "http://www.sleepingwolves.com/blog?page_id=319"); // seed the random number generator srand((double)microtime()*1000000); // get a random entry $randval = rand(0,count($linkarray)-1); // explode random entry into an array $entry = explode("|",$linkarray[$randval]); // print entry print ("<a href=\"$entry[0]\" target=\"_self\">Home</a>"); ?></li>
|
Paranoid (IV) Inmate From: A graveyard of dreams |
posted 12-17-2008 10:27
PHP otherwise has a function array_rand for getting random elements from an array. Using that the code would be: code: $link_array = array("http://www.sleepingwolves.com/blog?page_id=53", "http://www.sleepingwolves.com/blog?page_id=134", "http://www.sleepingwolves.com/blog?page_id=133", "http://www.sleepingwolves.com/blog?page_id=133", "http://www.sleepingwolves.com/blog?page_id=151", "http://www.sleepingwolves.com/blog?page_id=53", "http://www.sleepingwolves.com/blog?page_id=319"); $rand_key = array_rand($link_array); $rand_link = $link_array[$rand_key]; print '<a href="'.$rand_link.'" target="_self">Home</a>';
|
Paranoid (IV) Inmate From: |
posted 12-17-2008 23:20
Thanks will give it a spin. |