www.webdeveloper.com
Results 1 to 5 of 5

Thread: i need to make my quiz pick random questions instead of set ones

  1. #1
    Join Date
    Feb 2006
    Posts
    170

    i need to make my quiz pick random questions instead of set ones

    at the moment i have the following code for my quiz

    this bit gets the question

    PHP Code:
    <?php

    $queryq3 
    "SELECT QNO, Question, AnsA, AnsB, AnsC, AnsD, Answer FROM ip4quiz WHERE QNO ='3'"
       
       
    $resultq3 mysql_query($queryq3);

    while (
    $row mysql_fetch_array($resultq3)) {
       
    $questionno3 $row['QNO'];
       
    $question3 $row['Question'];
       
    $ansa3 $row['AnsA'];
       
    $ansb3 $row['AnsB'];
       
    $ansc3 $row['AnsC'];
       
    $ansd3 $row['AnsD'];
    }

    ?>
    then echos it out, the next bit marks it

    PHP Code:
    <?php $queryq2 "Select * from ip4quiz WHERE QNO ='2'";

    $resultq2 mysql_query($queryq2);

    while (
    $row mysql_fetch_array($resultq2)) {
       
    $qno2 $row['QNO'];
       
    $question2 $row['Question'];
       
    $ansa2 $row['AnsA'];
       
    $ansb2 $row['AnsB'];
       
    $ansc2 $row['AnsC'];
       
    $ansd2 $row['AnsD'];
       
    $ans2 $row['Answer'];
    }



        if (
    $question2result == $ans2) { 
            echo 
    "<table border=0 width=100%>";
            echo 
    "<tr><th><h3 align=left><img src=\"../images/quiz/quizcorrect.jpg\" />You answered Question: {$qno2} {$question2result} - This is Correct! </th></tr>\r\n"
            
    $score++; 
        } else { 
            echo 
    "<table border=0 width=100% bgcolor=003366>";
            echo 
    "<tr><th><h3 align=left><img src=\"../images/quiz/quizinincorrect.jpg\" />You answered Question: {$qno2} {$question2result} - This is Incorrect, The Correct answer is {$ans2}</th></tr>\r\n"
        



    ?>
    how can i make it pick from a list of questions at random instead of being told QNO = etc. my attempts have failed so far, any suggestions?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,933
    Via MySQL, you can randomly select one applicable row via:
    Code:
    SELECT [things to select and any where clause] ORDER BY RAND() LIMIT 1
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Feb 2006
    Posts
    170
    how could i pass that on so on the next page it could be marked?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,933
    Probably by putting the QNO value from the query into a hidden field on the question form so that the next page knows which question to "grade".
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Feb 2006
    Posts
    170
    so if i had another variable called $random question of soemthing, instead of the QNO ='2' bit - could i just have php make a random number for it then just POST it to the next page

    I've got it doing it using the following using

    srand(time());
    $randomqno1 = (rand()%10);

    to pick out a number each time then a hidden field passing the value on, the only thing i dont know now is how to stop the same number appearing each time
    Last edited by cashton2k; 02-26-2006 at 07:28 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles