Hi everyone! 🙂

I've been working on a PHP quiz. The quiz is database driven, all the questions are in a MySQL database. I want the questions to be displayed randomly, I'll also work in a timing system later to limit them to 10 minutes. I want to test true knowledge, not how quickly you can look stuff up :p

Anyway, some of the code I have so far ... I have the typical connection to the database, and a while loop to get all the information. Each question has an ID number. A question. 4 possible answers. And a correct answer (which will be a copy of one of the possible answers).

function NonDuplicateRandoms($num) {
  $arr = array();

  while( count($arr) < $num ) {
    $tmp = rand(0, 99);
    if( FALSE === array_search($tmp, $arr) )
      array_push($arr, $tmp);
  }

  return $arr;
}

This is my non-repeating random number function. Basically I ask for 10 random numbers (so I can get 10 questions) and this makes sure I don't get any repeated. Right now there are 100 questions.

To generate the questions, I do this:

$p = NonDuplicateRandoms(10);
$counter = 1;
$n=0;
while ($n<10) {

$i = $p[$n];
$display_block .= "
<tr><td valign=top>$counter</td>
<td>$question[$i]<br><input type=radio checked name=q$id[$i] value=\"$opt1[$i]\"> $opt1[$i]
<br><input type=radio name=q$id[$i] value=\"$opt2[$i]\"> $opt2[$i]
<br><input type=radio name=q$id[$i] value=\"$opt3[$i]\"> $opt3[$i]
<br><input type=radio name=q$id[$i] value=\"$opt4[$i]\"> $opt4[$i]
<p></td></tr>
";
$counter++;
$n++;
}

So if the number it gets is 23, it'll create an input named q23 and assign it the value of whichever radio button is checked.

My problem comes with trying to "grade" the quiz. For now I can deal with it just being 10 questions and not timed.

I can't figure out how to get the random $q variables to the next page. I can send over the $p array in a hidden input type, but I can't seem to figure out how to combined "q" and whatever p currently is to get $q23.

Any help would be appreciated 🙂

Thanks! 😃

    From your descriptions I am not totally clear about what you are actually trying to do....however from what I have deciphered I think this might help:

    so you have $q and you want to get the value of say whatever $q23 is. So if $q is the question array and $i the question number.

    $quest = "\$q" . "$i";

    $questionreturned = ${$quest};

      if they are in a mysql database you can do this much more easily

      $num = 10;
      $sql = "SELECT <id> FROM <table> ORDER BY rand()";
      $query = @mysql_query($sql);
      $id_list = array();
      $i=0;
      $j=0;
      while($i<$num && $j<mysql_num_rows($query)) {
          $id = @mysql_result($query,0,$j;
          if(!in_array($id_list),$id) {
              $id_list[$i] = $id;
              $i++:
          } //end if
          j++;
      } //end while
      
      if(count($id_list) < $num) {die("I need more questions in the database.");}
      

        $questionreturned = ${$quest};

        Aha, that's what I needed 😃 Thanks!!!

        Oh, and thanks for the Random number thing drawmack 🙂

          Write a Reply...