Posted: Sat May 03, 2008 8:12 pm Post subject: PHP/MySQL Quiz - retrieving random records
Hi everybody,
I have downloaded an old PHP/MySQL Quiz code from [url=http://www.getfreesofts.com/script/881/18744/PHPMySQLQuiz.html] GetFreeSofts.com[/url]. and it works well retrieving all records from a table. But when I try to retrieve random records (eg. 40 records of 100 records thar are stored in table) the answers aren't correctly.
Can anyone give me a help? Thanks!
Below are the table structure and the code.
[code:1]CREATE TABLE quiz (
id tinyint(4) NOT NULL auto_increment,
q text NOT NULL,
question text NOT NULL,
opt1 text NOT NULL,
opt2 text NOT NULL,
opt3 text NOT NULL,
answer text NOT NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;[/code:1]
[code:1]<?php
include("contentdb.php");
$display = mysql_query("SELECT * FROM $table ORDER BY RAND() LIMIT=40",$db);
echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";
}
elseif ($submit)
{
$score = 0;
$total = mysql_num_rows($display);
while ($result = mysql_fetch_array($display))
{
$answer = $result["answer"];
$q = $result["q"];
if ($$q == $answer)
{
$score++;
}
}
echo "<p align=center><b>You scored $score out of $total</b></p>";
echo "<p>";
if ($score == $total) {
echo "Congratulations! You got every question right!";
}
elseif ($score/$total < 0.34) {
echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
}
elseif ($score/$total > 0.67) {
echo "Well done! You certainly know your stuff.";
}
else {
echo "Not bad - but there were a few that caught you out!";
}
echo "</p>";
echo "<p>Here are the answers:";
echo "<table border=0>";
$display = mysql_query("SELECT * FROM $table ORDER BY RAND() LIMIT=40",$db);
while ($row = mysql_fetch_array($display)) {
if ($$q == $answer)
{
echo "<tr><td>»you answered ${$q}, which is correct</td></tr>";
}
elseif ($$q == "") {
echo "<tr><td>»you didn't select an answer. The answer is $answer</td></tr>";
}
else {
echo "<tr><td>»you answered ${$q}. The answer is $answer</td></tr>";
}
Posted: Wed May 07, 2008 2:48 am Post subject: Re: PHP/MySQL Quiz - retrieving random records
[quote="paulobetto"]But when I try to retrieve random records (eg. 40 records of 100 records thar are stored in table) the answers aren't correctly. [/quote]
Can you please explain this?
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum