Kramerica Industries, LLC

Random Image Example in PHP

This PHP script will place unique images in random order on your page.

<?php
function random_array($min, $max, $num){
   $range = 1+$max-$min;
   if($num > $range){
   	echo "here";
     return false;
   }
   $ret = Array();
   while(count($ret) < $num){
     $a = "1"; 
     do{
         $a = rand($min, $max);
   }while(in_array( $a, $ret));
     $ret[] = $a;
   }
   return $ret;
}

$_SESSION['pix_array'] = random_array(1, 4, 4);
?>
<div>
<?php 
	$locpix = $_SESSION['pix_array'];
	echo "<img src='$locpix[0]'.jpg >";
	echo "<img src='$locpix[1]'.jpg >";
	echo "<img src='$locpix[2]'.jpg >";
	echo "<img src='$locpix[3]'.jpg >";
?>
</div>



This script modified from one or more contained at http://us2.php.net/manual/en/function.rand.php