Log in

View Full Version : Resolved Randomizing questions in a form



k12onos
03-13-2012, 01:35 AM
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 :)

regicidedelferoz
03-13-2012, 01:38 AM
http://www.w3schools.com/php/func_math_rand.asp

k12onos
03-13-2012, 01:59 AM
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:


<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 :)

djr33
03-13-2012, 02:07 AM
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".

k12onos
03-13-2012, 02:29 AM
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 :)

djr33
03-13-2012, 03:01 AM
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.

k12onos
03-13-2012, 03:25 AM
Thanks for the clues, I'll look up to it and see if I can work something out :)

Again thanks a lot! :)