Jump to content


Photo

Random questions from mysql


  • Please log in to reply
3 replies to this topic

#1 marcus88

marcus88
  • New Members
  • Pip
  • Newbie
  • 2 posts
  • LocationEngland

Posted 02 March 2003 - 08:11 PM

Hello I am currently designing a web site for an on-line test with php and Mysql using apache server. I have to randomly pick questions from Mysql database via the server too displays on the main viewing screen. I would be grateful if you could point me in the right direction. Hope this makes sense because its starting to get me confuse I have just designed the main screen which generates the password with username.

Thanks


:?

#2 NL_Johan_UK

NL_Johan_UK
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 03 March 2003 - 12:20 AM

I assume you have all questions stored in tables already.

For a random question select, do this query first:

SELECT MIN(id), MAX(id) FROM your_question_table

I assume the ID is autoincremental, with no deleted records.

Then you have the minimum and maximum ID\'s. Use them for this:

srand((double)microtime()*1000000); 

$random_number = rand(0,100);

where 0 is your minimum and 100 your maximum...
Now select your random question:

SELECT question FROM your_question_table WHERE id = \'$random_number\'

There are many sources online on random strings and numbers, just search in google...

#3 marcus88

marcus88
  • New Members
  • Pip
  • Newbie
  • 2 posts
  • LocationEngland

Posted 03 March 2003 - 03:06 PM

Thanks

I’m having a problem with my database I’m not to sure how many fields to put I have a main screen for the users to view and there will have a choice of four questions to chose from with one being the right answer. Will I have to create four separate fields with 4 wrong answers and one right and how will it no to generate the correct answer? That’s the problem I’m having its driving my mad.

:oops:

#4 NL_Johan_UK

NL_Johan_UK
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 03 March 2003 - 03:37 PM

I think you mixed up some words like qestion and answer but let me try.

You only need to store the right answer.

I take it you use a drop down select option list , so all answers have a value. To see whether it was the right answer do this:

$query = "SELECT COUNT(*) FROM your_table WHERE correct_answer_field = $visitors_answer";

$result = mysql_query($query);

if(mysql_num_rows($result) < 1)

{

    echo\'they were wrong\';

}

else

{

   echo\'they guessed it right\';

}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users