SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random questions display

    Hello all,

    I've just started to write a LCMS where administrators can create and publish tests / quizzes for students.

    The first stumbling block I've come up against is how I'm going to display the possible answers in a random order.

    Here is how things are stored in the DB (MySQL):

    Question_id
    Question_text
    Correct_answer
    Alt_answer_1
    Alt_answer_2
    Alt_answer_3

    What I want to be able to do is echo out the Question text at the top of the page, but then randomise the possible answers so that the correct answer is not always at the top.

    I figured that hard coding place holders for the correct answer and alt answers is obviously the wrong way to go because then they'll always appear in their designated place.

    Can anyone offer some possible solutions to my problem.

    Any help would be greatly appreciated
    Thanks
    Matt

  2. #2
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might take a look at the rand() and microtime() functions and feed the answers into an array that can then be called via the array key. If that makes sense.

    Aaron
    Aaron Brazell
    Technosailor



  3. #3
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    You could also put the answers in an array and apply the
    shuffle function on the array. Then echo them out in a loop.

    <?php

    PHP Code:
     $answersArr = array('answer1''answer2''answer3');
     
    //srand for older version of php
     
    srand((float)microtime() * 1000000);
     
    shuffle($answersArr);
     foreach (
    $answersArr as $answer
     {
        echo 
    "$answer <br> ";
     } 
    ?>
    --ed

  4. #4
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nice one. How did I ever miss the shuffle() function?
    Aaron Brazell
    Technosailor



  5. #5
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Sketch
    nice one. How did I ever miss the shuffle() function?
    I don't know. PHP only has a gzillion builtin functions. You must not be very observant

    --ed

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help so far guys...

    I'll tell you how I get on.

    Cheers
    Matt


Bookmarks

Posting Permissions

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