I have a working quiz now, where I receive a selection of 3 random rows from a database currently of only 5 rows (expansion in progress). The database of format:
| id | question | option1 | option2 | option3 | answer |
--------------------------------------------------------
| 1 | What is my name? | Dave | Bob | Charles | Linda |
Within connect.php: (I am aware MySql statements are deprecated and will be updating to MySQLi in due course)
$query="SELECT * FROM (SELECT * FROM mytable ORDER BY rand() LIMIT 3) T1 ORDER BY id";
$result = mysql_query($query, $connect);
Then within quiz.php:
require (connect.php);
while($row = mysql_fetch_assoc($result)) {
echo '<p>';
echo $row['id'] . '. ';
echo $row['question'];
echo '</p>';
}
Radio buttons are generated within the while loop which associate the options with the generated question, this section works perfectly even with randomisation so is not included above.
Before I began attempting randomisation of the database row selections I could use the same while loop in quiz.php and quizresults.php as they would both select the same 5 rows which was perfect. But now I have decided to randomise the selection I can't use the same while loop within each page as it selects a random 3 rows for the quiz.php (the question section) and then selects a different random 3 rows for the quizresults.php which isn't ideal when the results don't match the questions...
So what I want to do is when the query is made in quiz.php, I want to assign all of $row 's values to a mutlidimensional array (i think) and then use that array within quizresults.php to echo those rows with all the values from the original query using I'm hoping the same format as within the results page.
Within quizresults.php:
require (connect.php);
while($row = mysql_fetch_assoc($result)) {
echo '<p>';
echo $row['id'] . '. ';
echo $row['question'];
echo '</p>';
}
It uses the same loop to print the questions but obviously with some radio button verification code, which I trimmed for the purpose of the question.
So to summarise, my question is: How can I achieve randomisation in the quiz.php (questions page) and then use the selection of random rows within the quizresults.php (answer page) without using the same query?
Aucun commentaire:
Enregistrer un commentaire