I already know how to do a random mysql/php query, but how do I track what has already been queried a row isn’t pulled again. Use case: A quiz that has 10 questions. The questions need to be in random order. How can I track that questions (rows) 2,5,6,8,9 have already been answered, but questions 1,3,4,7,10 remain. Here’s what I have so far:
$current_test = $_SESSION['testid'];
// v v v query for this TEST
$tests = mysql_query("SELECT * FROM the_tests WHERE the_tests.testid=$current_test");
$test = mysql_fetch_array($tests);
// ^ ^ ^ query for this TEST
// v v v query for the QUESTIONS from this Test
// Generate
if ($test['randomize']==1){
$offset_result = mysql_query("SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM the_questions WHERE the_questions.test_id_q=$current_test");//SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM the_questions WHERE `qid` NOT IN (1,5,6) AND the_questions.test_id_q=1
$offset_row = mysql_fetch_object($offset_result);
$offset = $offset_row->offset;
$questions = mysql_query("SELECT * FROM the_questions WHERE the_questions.test_id_q=$current_test LIMIT $offset, 1 " );
}else{
$questions = mysql_query("SELECT * FROM the_questions WHERE the_questions.test_id_q=$current_test");
}
$totalQuestions = mysql_query("SELECT * FROM the_questions WHERE the_questions.test_id_q=$current_test");
$totalQs = mysql_num_rows($totalQuestions);
$testQ = mysql_fetch_array($questions);
Thanks in advance!
Here’s what I came up with:
Assuming $_SESSION[‘qList’] is an array that looks like this:
When the user submits a question, there is a <input type=”hidden” value=”5″…> where the value is the current question ID. $eliminateQ below is this question ID:
This results in an updated qList array without ID number 5:
And the whole thing goes on until no questions are left. There is an if() statement in that instance forwarding the user on to the results page. Viola!
Thanks, @pixeline, for your insight. I took your NOT IN and reversed it to IN. 🙂