I am doing a quiz site using php and mysql wherein i am retrieving question and answers randomly from the database.Since the questions are getting repeated,i have decided to get the id of all the questions in an array and check for repeatability but dont know how to implement this using php..can anyone help me?
Problem With Array
Started by Divya, Mar 27 2009 10:57 PM
5 replies to this topic
#1
Posted 27 March 2009 - 10:57 PM
|
|
|
#2
Posted 28 March 2009 - 04:02 AM
the easiest thing would be to mark them used saved in a array in an session and randomize until you get a new one, then fetch that question from the database. make sure that the array is cleaned when all questions is asked. so it can start over, if that's what you want.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#3
Posted 29 March 2009 - 05:11 PM
The easiest way would be to randomize the mysql data and select the first N elements.
SELECT column FROM table ORDER BY RAND() LIMIT 10
#4
Posted 30 March 2009 - 02:01 AM
Yes, but as the author said in another thread (in database category) the same question showed up several times in a row when doing that. the array system I mentioned is to prevent that.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#5
Posted 30 March 2009 - 05:47 AM
In his other post, he was selecting one randomly item N times, and the probability of a duplicate result is N/S where S is the size of the set. If he selects N random items once, all N items are guaranteed to be random. Moreover, as N approaches S your algorithm becomes extremely inefficient.
For example, say he has 100 questions and he wants to generate 100 "random questions", his first question is guaranteed to be unique, his second question has a 99% chance of being unique. However his last question has a 99% chance of being a duplicate. Probabilistically, to get the last unique question, he will have to repeat your algorithm 100 times. Hypothetically, he might never generate the last question. In the end, your algorithm ran N(N+1)/2 times or on a set of 100 that equates to 5050 times which is not very optimal.
For example, say he has 100 questions and he wants to generate 100 "random questions", his first question is guaranteed to be unique, his second question has a 99% chance of being unique. However his last question has a 99% chance of being a duplicate. Probabilistically, to get the last unique question, he will have to repeat your algorithm 100 times. Hypothetically, he might never generate the last question. In the end, your algorithm ran N(N+1)/2 times or on a set of 100 that equates to 5050 times which is not very optimal.
#6
Posted 30 March 2009 - 08:30 PM
Hi all,
I have got the solution and i used array in a session as orjan said but if i try that query it gets repeated continuously because only one question should be loaded at once and on clicking the continue button it should move to the next question.Refer:quizpage.com
Thanks a lot for ur help.
Also,can anyone help me out to find the difference between the start time and end time of the quiz.
I have got the solution and i used array in a session as orjan said but if i try that query it gets repeated continuously because only one question should be loaded at once and on clicking the continue button it should move to the next question.Refer:quizpage.com
Thanks a lot for ur help.
Also,can anyone help me out to find the difference between the start time and end time of the quiz.


Sign In
Create Account


Back to top









