View Full Version : Random Numbers - PHP to HTML
almost
03-29-2005, 08:25 PM
I'll probably feel even more ridiculous when someone answers, but this is all pretty new to me. I have a working PHP script that I want to modify so that the starting point in an array (of images to be displayed) is determined randomly. I think I know how to generate the number in PHP, but I don't know how to pass it on to the regular HTML "var" variable which is used to index the array.
How do I transfer a PHP variable's value to an HTML variable?
<php? $RandomNum = rand(0, array.length - 1)?>
var IndexNum = ????
Justin
03-29-2005, 09:18 PM
I'll probably feel even more ridiculous when someone answers, but this is all pretty new to me. I have a working PHP script that I want to modify so that the starting point in an array (of images to be displayed) is determined randomly. I think I know how to generate the number in PHP, but I don't know how to pass it on to the regular HTML "var" variable which is used to index the array.
How do I transfer a PHP variable's value to an HTML variable?
You can use array_rand() (http://php.net/array_rand)
<?php
srand((float) microtime() * 10000000);
$array = array("Mike", "John", "Bob", "Jim");
$random = array_rand($array);
print $array[$random];
?>
Now as far as passing arrays in URLs, just use serialize() (http://php.net/serialize). Then, use unserialize() (http://php.net/unserialize) to compile it back into an array.
almost
03-29-2005, 10:45 PM
Thanks. That'll solve both my immediate and my soon-to-come problems!
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.