Public feed
#377390
<?phpfunction randomselection($c, $sql) { // run the query $result=mysql_query($sql) or die($sql); // get the upper limit for rand() $up = mysql_num_rows($result)-1; // check that there is more data than rows required if ($up <= $c) { while ($row=mysql_fetch_assoc($result)) { $selection[] = $row; } return $selection; } // set up array to hold primary keys and elliminate duplicates // since random DOES NOT mean unique // or the query may not return DISTINCT rows $keys = array(); // get random selection while (count($keys) < $c) { // get random number for index $i = rand(0, $up); // move pointer to that row number and fetch the data mysql_data_seek($result, $i); $row = mysql_fetch_assoc($result); // check if the primary key has already been fetched if (!in_array($row['id'], $keys)) { // store the row $selection[] = $row; // store the id to prevent duplication $keys[] = $row['id']; } } return $selection;}?>