Forum Moderators: coopster

Message Too Old, No Replies

I'm trying to make a quiz using php and mysql.

         

coletteno1

4:36 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



Hi Guys,
I'm new to the forums so sorry if I make any mistakes!
I'm trying to make a quiz using php and mysql.
There are going to be hundreds of questions and so to select 50 random ones I am going to use a select statement to get 50 random questions
SELECT * FROM questions ORDER BY RAND( ) LIMIT 50
But I am quite inexperienced when it comes to using PHP, I'm trying to think of the best way to show these one by one to the user.
I was thinking of inserting the 50 random questions into another table and then printing them one by one after a question has been answered.
I'm sure there must be a better way of doing this? I guessing using arrays but I haven't used them before so I'm not sure if it would be suitable.

Any help would be appreciated or any links where I can get more information.
Thanks in advance!

londrum

5:28 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if you put them into a temporary table you'd have to do the same for every user, which could cause you problems if you have loads of users.
i would do it like this...
select one random question and then put that row number (or ID number, or whatever) into a cookie.
everytime they need a new question all you've got to do is select a new random one, minus whatever rows/IDs are stored in the cookie.
you can store more than one number in the same cookie pretty easy just by adding the new number onto the end of the cookie with a dot, or a slash, or a comma before it. then when you come to check the numbers you just explode it at whatever symbol you have chosen.

andrewsmd

9:43 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could use php to generate a query like so. It's not pretty but it works.

<html>
<?php

//an array to store unique numbers
$numArr = array();

//while our array does not have 51 numbers in it
//we keep getting a random number and adding it to
//the array as long as it is not already in the array
while(count($numArr) < 50){

//our random number based on the question ids
//I'm assuming they are from 0 to 99
$tempRand = rand(0, 99);

//if the number is in the array get a new one
while(array_search($tempRand, $numArr)){

$tempRand = rand(0, 99);

}//while

array_push($numArr, $tempRand);


}//while

//start the sql query
$query = "select * from questions where ";

$total = count($numArr);

$num = 1;

//now loop through the array and generate and sql query
foreach($numArr as $i){

//if num is less than total then we need to add
//an or on the end of the query otherwise we finish it
if($num < $total){

$query .= "questionid = $i OR ";
$num++;

}//if

else{

$query .= "questionid = $i;";

}//else

}//foreach

//i echoed the query to see what it was
//but here you would query your db to get all of your
//questions.
echo($query);

?>
</html>