I’m creating a randomised test. This test asks, say, 25 questions from a question bank containing many more questions. The obvious solution would be to generate a list of unique random numbers and use that to pull out a specific questionid from the question database. The problem with this is that it’s rarely possible to be certain that every questionid exists as questions are removed during editing.
The simplest way of getting past this is to use mysql_data_seek(data,whichrow) to find the row indicated by each of the random numbers and pull out of that the questionid.
for($n=0;$n<$numberofrecords;$n++)
{
$thisrow=$sequence[$n]; //read out the random number from the sequence (it’s an array)
//move the pointer to the row in the database that relates to that question ($questiondata is
//the result of a mysql_query
mysql_data_seek($questiondata,$thisrow);
//pull out the associative array for that row
$questiondataarray=mysql_fetch_assoc($questiondata);
$newsequence.=$questiondataarray['questionid'].”,”;
}
…and you then end up with a string ($newsequence) containing a series of valid question ids.




.gif)