Results 1 to 7 of 7

Thread: Randomizing questions in a form

  1. #1
    Join Date
    Aug 2006
    Posts
    58
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Smile Randomizing questions in a form

    Hi

    I'm trying to make a HTML/PHP quiz using forms, but now i'm searching for a way to randomize the question order.

    So let's say there's 10 different questions. #1 - #10. Each question would include HTML codes for the form and pictures.

    I want it to randomize and change order each time the user re-do the test (in essence: refreshing the page).

    So the first time it loads the order may be

    #2 #8 #5 #7 #6 #4 #1 #3 #10 #9

    The second time

    #4 #3 #7 #10 #1 #9 #2 #8 #5 #6

    The important thing is that there is always 10 different questions in random order.

    Is there any javascripts or preferrably PHP script that can do this?

    I tried working it out from an image randomizing script but I guess my skill isn't good enough.


    Thank you in advance, I would very much appreciate your help
    Last edited by k12onos; 03-20-2012 at 12:09 PM.

  2. #2
    Join Date
    Jun 2011
    Location
    Pearl of the East
    Posts
    53
    Thanks
    1
    Thanked 2 Times in 2 Posts

  3. #3
    Join Date
    Aug 2006
    Posts
    58
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default

    Hi, sorry but I was not trying to randomize the numbers

    The #2 #3 #4 was just an example,

    each question will contain something like this:

    HTML Code:
    <h3>CSS Stands for...</h3>
                        
                        <div>
                            <input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
                            <label for="question-1-answers-A">A) Computer Styled Sections </label>
                        </div>
    
                         <div>
                            <input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
                            <label for="question-1-answers-B">B) Cascading Style Sheets</label>
                        </div>
                        
                        <div>
                            <input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
                            <label for="question-1-answers-C">C) Crazy Solid Shapes</label>
                        </div>
                        
                        <div>
                            <input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
                            <label for="question-1-answers-D">D) None of the above</label>
                        </div>

    So I will have 10 of those, And I need to randomize the order they come in the page.

    But anyway thanks for replying

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Do you want all of the questions to be displayed at once time on the page?

    The easiest way will be to use an array with numerical indices (or 'keys') then use a random number sequence to select which one to display when. That works in either JS or PHP. If you do use PHP, then shuffle() is convenient for randomizing the order of arrays.

    It's the same logic if you want these displayed on one page after another, but you'd need to also track which questions have been shown-- using a PHP session is probably the best way. You could use cookies in JS, but that's not entirely reliable.

    If this is anything that should be "secure" then you need PHP (or another serverside language). JS can be 'hacked' (manipulated freely) so don't rely on it, but it'll certainly work if you don't care about it being completely "secure".
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    k12onos (03-20-2012)

  6. #5
    Join Date
    Aug 2006
    Posts
    58
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default

    The question will be displayed all in one page (though I will use jQuery Cycle to make them seem to appear 1 by 1 in the eyes of the user).

    Is there a readily made script in dynamic drive with similar logic as this? That is, a randomizing script from an array containing HTML excerpts?

    I'm still learning here and I don't have the skills to start from scratch, but would probably be able to make modifications to existing ones. Any clue what kind of scripts I should search? the keyword perhaps?

    Thank you

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    1. Create your array (type this out by hand).
    2. Create an array of just the indices/keys (or generate it automatically if you prefer-- probably faster to not worry about that yet).
    3. Randomize that array of indices: https://www.google.com/search?q=shuffle+array+js (a few tutorials there should help)
    4. Use those numbers, in a "for" loop to set the initial order. (Go through each element in the array based on its key, and you'll be able to output the randomly-ordered item in the array. Doing that is easy-- just use the output of that as the index/key for the array of questions.

    So, you'll be using two arrays, one containing the indicies/keys of the other, which themselves will then be used again as keys after being randomized.

    And, no, there isn't a premade script for this because it's relatively easy to do-- it's just logical code, not a "script" (in the way that, for example, an image gallery would be). But if you want to search for an existing "javascript quiz" there are plenty, and I believe one is here on DD.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. The Following User Says Thank You to djr33 For This Useful Post:

    k12onos (03-20-2012)

  9. #7
    Join Date
    Aug 2006
    Posts
    58
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the clues, I'll look up to it and see if I can work something out

    Again thanks a lot!

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
  •