JosephHanh Posted October 16, 2014 Share Posted October 16, 2014 I am working on a quiz appimage 1 shows the index.php pageimage 2 shows the first questionimage 3 shows the second questionimage 4 shows the third questionimage 5 shows the result after completing the quizimage 6 shows the database 'quizzer' and its tablesimage 7 shows the 'questions' tableimage 8 shows the 'choices' tableTHIS LINK CONTAIN ALL THE CODE (and images) I HAVE DONE SO FARhttps://www.mediafire.com/folder/g5ao7f5q0fe6y/quiz1.Now my question is how to select the question RANDOMLY from 'questions' table along with 'choices' (by adding code to the existing file or create a new one).2.If user refresh/reload the page before starting ('Start Quiz') or click 'Take Again' after finishing the quiz, the question should appear randomly.3.Basically I want to change the order of question appearing in the browser each time I refresh.4.My work so far is mentioned above.........Please help me with this "RANDOM" problem !!P.S - Will it be possible, by creating a random function in PHP which will check for repeat questions in a session and check for the 'id' of the question and if it is new display it on the page.If so what should I do and if no then how to do? Quote Link to comment https://forums.phpfreaks.com/topic/291757-how-to-select-question-from-mysql-tables-randomly/ Share on other sites More sharing options...
CroNiX Posted October 16, 2014 Share Posted October 16, 2014 You can just store the questions in an array and use shuffle() on the array. You can also use ORDER BY RAND() in your sql query, although I've read it's not as good in the past. 1 Quote Link to comment https://forums.phpfreaks.com/topic/291757-how-to-select-question-from-mysql-tables-randomly/#findComment-1493920 Share on other sites More sharing options...
Frank_b Posted October 16, 2014 Share Posted October 16, 2014 or this query: SELECT * FROM pictures ORDER BY RAND() LIMIT 1; Quote Link to comment https://forums.phpfreaks.com/topic/291757-how-to-select-question-from-mysql-tables-randomly/#findComment-1493934 Share on other sites More sharing options...
jcbones Posted October 16, 2014 Share Posted October 16, 2014 Don't use ORDER BY RAND(), if you want random from the database, check http://http://explainextended.com/2009/03/01/selecting-random-rows/ first. After that, I like Cronix suggestion, using the session to store used question id's, so you can check against it for repeat questions. Quote Link to comment https://forums.phpfreaks.com/topic/291757-how-to-select-question-from-mysql-tables-randomly/#findComment-1493944 Share on other sites More sharing options...
Barand Posted October 17, 2014 Share Posted October 17, 2014 Depends on how large the table is. That article is talking about millions of rows. With small tables the performance of ORDER BY RAND() should be acceptable mysql> SELECT COUNT(*) FROM votes; +----------+ | COUNT(*) | +----------+ | 182685 | +----------+ 1 row in set (0.08 sec) mysql> SELECT * FROM votes -> ORDER BY RAND() -> LIMIT 10; +--------+-------+------+ | id | votes | type | +--------+-------+------+ | 153136 | 141 | 15 | | 166164 | 646 | 5 | | 58917 | 742 | 19 | | 164668 | 273 | 13 | | 87220 | 773 | 3 | | 85013 | 600 | 18 | | 137931 | 662 | 6 | | 119082 | 1025 | 7 | | 84811 | 480 | 9 | | 111548 | 326 | 3 | +--------+-------+------+ 10 rows in set (0.18 sec) Quote Link to comment https://forums.phpfreaks.com/topic/291757-how-to-select-question-from-mysql-tables-randomly/#findComment-1493971 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.