[PHP] Random questions from database

2004-01-27 Thread Alex Hogan
Hi All,

 

I want to have a series of questions that are randomized for a particular
lesson.  This part is easy.

 

I have taken the results from the db and put it in an array and then used
shuffle() to randomize the questions.

 

while($row = mssql_fetch_array($result))

{

$id[] = $row['id'];

$ques[] = $row['question'];

}

 

srand((float)microtime()*100);

shuffle($ques);

 

foreach($ques as $ranQues){

echo trtd$ranQues /td/tr\n;

}

 

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 trtda href=\$id\$ranQues/a/td/tr\n;

}

 

My problem is keeping the $id with the $ranQues after I do the shuffle.

 

I think I am on a solid approach but if there is a simplier way please let
me know.

 

 

 

alex hogan

 



** 
The contents of this e-mail and any files transmitted with it are 
confidential and intended solely for the use of the individual or 
entity to whom it is addressed.  The views stated herein do not 
necessarily represent the view of the company.  If you are not the 
intended recipient of this e-mail you may not copy, forward, 
disclose, or otherwise use it or any part of it in any form 
whatsoever.  If you have received this e-mail in error please 
e-mail the sender. 
** 




Re: [PHP] Random questions from database

2004-01-27 Thread memoimyself
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()*100);

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 trtda 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 trtda href=\$ranQues['id']\$ranQues['ques']/a/td/tr\n;
}

Hope this helps.

Erik