Jump to content

Problem With Array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Divya

Divya

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
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?

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
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

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
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
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
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

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
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.

#6
Divya

Divya

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
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.