Topic: Help convert JS to PHP (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=30711" title="Pages that link to Topic: Help convert JS to PHP (Page 1 of 1)" rel="nofollow" >Topic: Help convert JS to PHP <span class="small">(Page 1 of 1)</span>\

 
SleepingWolf
Paranoid (IV) Inmate

From:
Insane since: Jul 2006

posted 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>



The Link:

code:
<div id="cont" align="center"><a href="javascript:randomlink()">




How would I do this in PHP to be used in WordPress - hopefully without embedding the js.

I did my STFW, but googling "random" is not a good idea.

Nature & Travel Photography
Main Entrance

(Edited by SleepingWolf on 12-17-2008 01:27)

SleepingWolf
Paranoid (IV) Inmate

From:
Insane since: Jul 2006

posted 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>



Nature & Travel Photography
Main Entrance

(Edited by SleepingWolf on 12-17-2008 02:37)

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted 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>';



_________________________
- the Golden Ratio

SleepingWolf
Paranoid (IV) Inmate

From:
Insane since: Jul 2006

posted posted 12-17-2008 23:20

Thanks will give it a spin.

Nature & Travel Photography
Main Entrance



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu