note 70844 deleted from function.rand by felipe

From: Date: Sun, 30 Dec 2007 18:45:08 +0000
Subject: note 70844 deleted from function.rand by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-134443@lists.php.net to get a copy of this message
Note Submitter: euan_m at yahoo dot fr ---- You could try this, it works fine : <?php $connection = mysql_connect ($host, $login, $password); mysql_select_db ($DataBase, $connection) ; $c = 5 ; // Whatever.. protected function randomselection($c, $connection) { $data = getdata($connection) ; // Get your datas somewhere you like to $totalcount = count($data) ; $j = array() ; if ($c > $totalcount) { $newdata = $data ; } // if not enough datas rows in $data else { for($i = 0; $i <= $c; $i++) { $exists = 1 ; for($exists > 0; $exists++;) { $test = mt_rand(1,$totalcount) ; // depends on where you want to start - can also use rand() if(!in_array( $test , $j )) { $j[$i] = $test ; $exists = 0 ; } } $newdata[$i] = $data[$j[$i]] ; } } return $newdata ; } ?>

« previous php.notes (#134443) next »