Re: Random questions from database

From: Date: Tue, 27 Jan 2004 17:01:51 +0000
Subject: Re: Random questions from database
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-175946@lists.php.net to get a copy of this message
Hello Alex, On 27 Jan 2004 at 9:25, Alex Hogan wrote: > while($row = mssql_fetch_array($result)) > > { > > $id[] = $row['id']; > > $ques[] = $row['question']; > > } To keep question and id together even after shuffling the array, why don't you build an array of arrays like this: $i = 0; $questions = array(); while( $row = mssql_fetch_array($result) ) { $questions[$i]['ques'] = $row['question']; $questions[$i]['id'] = $row['id']; $i++; } > srand((float)microtime()*1000000); You don't need to seed the random number generator if you're using PHP 4.2.0 or above. > shuffle($ques); > > (...) > > What I'm wanting to do now is to add the id for each of the questions so > that I can track which one is the correct answer and which are the > distractors. I will have something that looks like this. > > > Foreach($ques as $ranQues){ > > echo "<tr><td><a > href=\"$id\">$ranQues</a></td</tr>\n"; > > } > > > My problem is keeping the $id with the $ranQues after I do the shuffle. That's not a problem if you follow my previous suggestion. All you need to do is: foreach($questions as $ranQues) { echo "<tr><td><a href=\"$ranQues['id']\">$ranQues['ques']</a></td</tr>\n"; } Hope this helps. Erik

« previous php.general (#175946) next »