Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more  

Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Web Programming > PHP Development
User Name
Password

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-03-2008, 02:38 PM   #1 (permalink)
paulobetto
Junior Member
 
Join Date: May 2008
Posts: 1
Default PHP/MySQL Quiz - retrieving random records

Hi everybody,

I have downloaded an old PHP/MySQL Quiz code from GetFreeSofts.com 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:
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:
<?php include("contentdb.php"); $display = mysql_query("SELECT * FROM $table ORDER BY RAND() LIMIT=40",$db); if (!$submit) { echo "<form method=post action=$PHP_SELF>"; echo "<table border=0>"; while ($row = mysql_fetch_array($display)) { $id = $row["id"]; $question = $row["question"]; $opt1 = $row["opt1"]; $opt2 = $row["opt2"]; $opt3 = $row["opt3"]; $answer = $row["answer"]; echo "<tr><td colspan=3><br><b>$question</b></td></tr>"; echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>"; } 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)) { $question = $row["question"]; $answer = $row["answer"]; $q = $row["q"]; echo "<tr><td><br>$question</td></tr>"; if ($$q == $answer) { echo "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>"; } elseif ($$q == "") { echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>"; } else { echo "<tr><td>&raquo;you answered ${$q}. The answer is $answer</td></tr>"; } } echo "</table></p>"; } ?>
paulobetto is offline   Reply With Quote
Sponsored Links
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Points Per Thread View: 1.00
Points Per Thread: 11.00
Points Per Reply: 5.00



» Sponsors

» Links

» Affiliates
Web Hosting
Marketing Find
Merchant Select
SiteMap Builder
Host Compare
Dedicated Servers

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 07:21 AM.



LinkBacks Enabled by vBSEO 3.0.0 RC8
Webmaster Forums