|
Posted by Andrew @ Rockface on 06/24/05 13:27
In news:d9gm5n$cah$1@ss405.t-com.hr,
CyberDog <milan-SPAM-@cdnforum.com> wrote:
> I am making an quiz script, and I cam accross a problem:
>
> example:
> I have a database that holds 10 questions.
> User is given only 5 random questions.
> How do I make sure that the same question is not displayed twice?
>
> I use $question = rand(1, $number) where $number is mysql_num_rows of
> the question database.
>
> so I have a
>
> while($position < $displayed_questions)
> {
>
> $question = rand(1, $number);
>
> // database query stuff
> // template loading stuff
>
> $display .= $question
>
> $position++;
> }
>
> But no way to make sure that the same question isn't taken twice...
>
> Any ideas? :(
Build an array of every possible number (count the number of questions),
then shuffle the array.
OR
As each number is generated record it in an array, then check each new
number against the elements in the array.
OR
Generate the 5 random numbers and store them in an array. Check that each
number is different and if not then generated a new number.
--
Andrew @ Rockface
np: 1000 Suns - 5 [stopped]
www.rockface-records.co.uk
Navigation:
[Reply to this message]
|