array_rand can be used to select a list of values randomly. This helped me to code a server side script to display a random grid of pictures as part of a project. These pictures changes every time the page is requested or refreshed.


Below is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
srand((float) microtime() * 10000000);
$input = array("lion.gif", "cat.gif", "dog.gif", "bird.gif", 
"tiger.gif", "eagle.gif");
$rand_keys = array_rand($input, 4);
?>
 
<table align="center" cellspacing="15">
<tr>
<td  bgcolor="#f0f0f0" align="middle">
<img src="<?php echo $input[$rand_keys[0]] ?>">
</td>
<td  bgcolor="#f0f0f0" align="middle">
<img src="<?php echo $input[$rand_keys[1]] ?>">
</tr>
<tr>
<td  bgcolor="#f0f0f0" align="middle">
<img src="<?php echo $input[$rand_keys[2]] ?>">
</td>
<td  bgcolor="#f0f0f0" align="middle">
<img src="<?php echo $input[$rand_keys[3]] ?>">
</tr>
</table>

Leave a Reply

*
Haider’s WebSpace
Welcome to my technical blog. This is where I write, archive and share computer related articles. Subjects vary from posting technical solutions to researching particular topics. Feel free to comment and talk IT!
Sponsored Links
My Tweets
Posts Calendar
February 2012
M T W T F S S
« Nov    
 12345
6789101112
13141516171819
20212223242526
272829