PHP forums, MySQL forums, Web Development resources

Home PageHome    PHP ResourcesTopic List    FAQFAQ    SearchSearch    MemberlistMemberlist    UsergroupsUsergroups 
 RegisterRegister
    ProfileProfile    Log in to check your private messagesLog in to check your private messages    Download the RSS Reader RSS Feed Download the RSS Reader RSS for this forum Log inLog in 

PHP Forum :: MySQL Forum :: Java Script Forum



PHP/MySQL Quiz - retrieving random records

 
Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General
View previous topic :: View next topic  
Author Message
paulobetto



Joined: 03 May 2008
Posts: 1

PostPosted: Sat May 03, 2008 8:12 pm    Post subject: PHP/MySQL Quiz - retrieving random records Reply with quote

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>";



}

?>
Back to top
View user's profile Send private message
mwasif



Joined: 22 Mar 2008
Posts: 28

PostPosted: Wed May 07, 2008 2:48 am    Post subject: Re: PHP/MySQL Quiz - retrieving random records Reply with quote

paulobetto wrote:
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 you please explain this?
_________________
PHP, Apache, MySQL
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
WeberTrivia Questions WeberTrivia Questions
 Think you are smart? Prove it!. Try your skills with these questions :
 WeberTrivia QuestionsRecursive arrays and multi-dimensional arrays are one and the same. (PHP and MySQL)
 WeberTrivia QuestionsThe \"cache_dir\" tag of the squid configuration has a default of /var/spool/squid. (Linux)

WeberTrivia Questions



Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
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






Powered by phpBB © 2001, 2005 phpBB Group
PHP Forum :: MySQL Forum :: Java Script Forum