Page 1 of 1

i made a quizz and i want to show a tables with the results

Posted: Tue Mar 31, 2009 7:35 pm
by sebis
hello.I have a code that displays a quizz which asks you to enter your first name and answer the questions.when u hit done then a new page is displayed showing the correct answers and how many did you find.Wjat i would like is to gather the score and first name information to a database table in Mysql and show a table with the names and scores starting with the higher score.can anyone help me?
these are the two codes for the quizz

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>PHP quiz</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body bgcolor="#CCFFCC">
<center>
  <H2><font color="#000099" face="Comic Sans MS">French History Quiz </font></H2>
</center>
<form action=http://turul.eet.bme.hu/~bkpl/french%20history%20site/quizaction.phtml
method="POST"> 
<ul>
<FONT COLOR="#CC0000"><b>Please click in the text fields below and enter the information requested.</b></FONT> 
<ul><ul>
<li><font face="Comic Sans MS">First name:</font>
  <INPUT TYPE="TEXT" NAME="firstname" size="35"></li>
<br>
</ul></ul>
<HR>
<UL><B><FONT COLOR="#CC0000">Answer the questions below.&nbsp; Your individual responses will be kept confidential.<br>
For each item, select an option. When finished, click the "Done" button.</FONT></B></UL>
<UL>
  <strong>1. When was the French Revolution?  </strong>
  <UL>
<INPUT TYPE="RADIO" NAME="question1" VALUE="answer1.1">1900<BR>
<INPUT TYPE="RADIO" NAME="question1" VALUE="answer1.2">1789<BR>
<INPUT TYPE="RADIO" NAME="question1" VALUE="answer1.3">1800<BR>
<INPUT TYPE="RADIO" NAME="question1" VALUE="answer1.4">1700<BR>
</UL></UL>
<HR>
<UL>
  <strong>2. What is the name of the French national anthem?  </strong>
  <UL>
<INPUT TYPE="RADIO" NAME="question2"VALUE="answer2.1">marseilles<BR>
<INPUT TYPE="RADIO" NAME="question2"VALUE="answer2.2">parisien<BR>
<INPUT TYPE="RADIO" NAME="question2" VALUE="answer2.3">chti mi<br>
<INPUT TYPE="RADIO" NAME="question2"VALUE="answer2.4">None of the above<BR>
</UL></UL>
<HR>
<UL>
  <strong>3. Who is the president of France?  </strong>
  <UL>
<INPUT TYPE="RADIO" NAME="question3"VALUE="answer3.1">Francois Mitterand<BR>
<INPUT TYPE="RADIO" NAME="question3"VALUE="answer3.2">Nicola Sharkozy<BR>
<INPUT TYPE="RADIO" NAME="question3" VALUE="answer3.3">Jack Shirack<br>
<INPUT TYPE="RADIO" NAME="question3"VALUE="answer3.4">None of the above<BR>
</UL></UL>
<HR>
<UL>
  <strong>4. When was the Eiffel Tower build?  </strong>
  <UL>
<INPUT TYPE="RADIO" NAME="question4"VALUE="answer4.1">1889<BR>
<INPUT TYPE="RADIO" NAME="question4"VALUE="answer4.2">1950<BR>
<INPUT TYPE="RADIO" NAME="question4" VALUE="answer4.3">1800<br>
<INPUT TYPE="RADIO" NAME="question4"VALUE="answer4.4">1850<BR>
</UL></UL>
<HR>
<UL>
  <strong>5. When was the battle of Waterloo? </strong>
<UL>
<INPUT TYPE="RADIO" NAME="question5"VALUE="answer5.1">1800<BR>
<INPUT TYPE="RADIO" NAME="question5"VALUE="answer5.2">1780<BR>
<INPUT TYPE="RADIO" NAME="question5" VALUE="answer5.3">1815<br>
<INPUT TYPE="RADIO" NAME="question5"VALUE="answer5.4">1900<BR>
</UL></UL>
<HR>
<UL>
  <strong>6. Where was Napoleon exiled after his defeat? </strong>
<UL>
<INPUT TYPE="RADIO" NAME="question6"VALUE="answer6.1">Corsica<BR>
<INPUT TYPE="RADIO" NAME="question6"VALUE="answer6.2">Cyprus<BR>
<INPUT TYPE="RADIO" NAME="question6" VALUE="answer6.3">Saint Helena<br>
<INPUT TYPE="RADIO" NAME="question6"VALUE="answer6.4">Magadascar
</UL></UL>
<HR>
<UL>
    <strong>7. Who was the leader of the French army in WWII? </strong>
<UL>
<INPUT TYPE="RADIO" NAME="question7" VALUE="answer7.1">Nikolas Sharkozy<BR>
<INPUT TYPE="RADIO" NAME="question7" VALUE="answer7.2">Charles De Gaulle<BR>
<INPUT TYPE="RADIO" NAME="question7" VALUE="answer7.3">Alain Delon<BR>
<INPUT TYPE="RADIO" NAME="question7" VALUE="answer7.4">Napoleon Bonaparte<BR>
</UL></UL>
<HR>
<UL>
    <strong>8. What was the slogan in the French Revolution? </strong>
<UL>
<INPUT TYPE="RADIO" NAME="question8" VALUE="answer8.1">Bread, Education, Freedom<BR>
<INPUT TYPE="RADIO" NAME="question8" VALUE="answer8.2">Liberte, Egalite, Fraternite<BR>
<INPUT TYPE="RADIO" NAME="question8" VALUE="answer8.3">Democracy, Freedom, Eqalite<BR>
<INPUT TYPE="RADIO" NAME="question8" VALUE="answer8.4">Live free or die<BR>
</UL></UL>
<HR>
<UL>
    <strong>9. Who was Charlemagne? </strong>
<UL>
<INPUT TYPE="RADIO" NAME="question9" VALUE="answer9.1">King of Francks in 768<BR>
<INPUT TYPE="RADIO" NAME="question9" VALUE="answer9.2">The horse of Napoleon<BR>
<INPUT TYPE="RADIO" NAME="question9" VALUE="answer9.3">Famous french footballer<BR>
<INPUT TYPE="RADIO" NAME="question9" VALUE="answer9.4">Leader of the French Revolution<BR>
</UL></UL>
<HR>
<UL>
    <strong>10. How did Joan of Arc died? </strong>
<UL>
<INPUT TYPE="RADIO" NAME="question10" VALUE="answer10.1">By hanging<BR>
<INPUT TYPE="RADIO" NAME="question10" VALUE="answer10.2">By drowning<BR>
<INPUT TYPE="RADIO" NAME="question10" VALUE="answer10.3">By burning<BR>
<INPUT TYPE="RADIO" NAME="question10" VALUE="answer10.4">By natural causes<BR>
</UL></UL>
  <BR>
</UL>
<ul><ul>
  <font face="Comic Sans MS">
<input name="submit" type="submit" class="style49" value="done" />
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
  </font>
</ul>
</ul>
</FORM>
<hr>
</body>
</html>
 

Code: Select all

#!/usr/local/bin/php
<html>
<head><title>Quiz Feedback</title></head>
<body bgcolor="#CCFFCC">
<?php
$score = 0; //initialize score to zero
?>
<h3>Quiz Feedback</h3>
Thank you, <font color="Comic Sans Ms"><b>
<?php 
$firstname = $_POST['firstname']; 
$question1 = $_POST['question1'];
$question2 = $_POST['question2'];
$question3 = $_POST['question3'];
$question4 = $_POST['question4'];
$question5 = $_POST['question5'];
$question6 = $_POST['question6']; 
$question7 = $_POST['question7'];
$question7 = $_POST['question8'];
$question7 = $_POST['question9'];
$question7 = $_POST['question10'];
echo ($firstname); 
?> </b></font>.
<hr>
<h4>Question 1 Feedback</h4>
When was the French Revolution.
<p><ul>Answer: 1789<br>
Your answer was: 
<?php
if ($question1 == "answer1.2")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?></ul>
<hr>
<h4>Question 2 Feedback</h4>
What is the name of the French national anthem?
<p><ul>Answer: marseilles<br>
Your answer was: 
<?php
if ($question2 == "answer2.1")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?></ul>
<hr>
<h4>Question 3 Feedback</h4>
Who is the president of France?
<p><ul>Answer: Nicola Sharkozy<br>
Your answer was: 
<?php
if ($question3 == "answer3.2")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?></ul>
<hr>
<h4>Question 4 Feedback</h4>
When was the Eiffel Tower build?
<p><ul>Answer: 1889<br>
Your answer was: 
<?php
if ($question4 == "answer4.1")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?></ul>
<hr>
<h4>Question 5 Feedback</h4>
When was the battle of Waterloo?
<p><ul>Answer: 1815<br>
Your answer was: 
<?php
if ($question5 == "answer5.3")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?></ul>
<hr>
<h4>Question 6 Feedback</h4>
Where was Napoleon exiled after his defeat?
<p><ul>Answer: Saint Helena<br>
Your answer was: 
<?php
if ($question6 == "answer6.3")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?></ul>
<hr>
<h4>Question 7 feedback</h4>
Who was the leader of the French army in WWII?
<p><ul>Answer: Charles De Gaulle<br>
Your answer was:
<?php
if ($question7 == "answer7.2")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?></ul>
<hr>
<h4>Question 8 feedback</h4>
What was the slogan in the French Revolution? 
<p><ul>Answer: Liberte, Egalite, Fraternite<br>
Your answer was:
<?php
if ($question7 == "answer8.2")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?></ul>
<hr>
<h4>Question 9 feedback</h4>
Who was Charlemagne? 
<p><ul>Answer: King of Francks in 768<br>
Your answer was:
<?php
if ($question7 == "answer9.1")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?></ul>
<hr>
<h4>Question 10 feedback</h4>
How did Joan of Arc died? 
<p><ul>Answer:By burning<br>
Your answer was:
<?php
if ($question7 == "answer10.3")
{
echo ("<b>Correct</b>");
$score = $score + 1;
}
else
{
echo ("<b>Incorrect</b>");
}
?>
</ul></ul>
<hr>
<b>Total score:</b>You answered <?php echo ($score); ?> question
<?php
if ($score != 1)
{
echo ("s");
}
?> correctly.
<hr>
</body>
</html>

Re: i made a quizz and i want to show a tables with the results

Posted: Tue Mar 31, 2009 8:17 pm
by tech603
Here is a little bit of code that might help you along your way, you'll notice that i wrapped the $_POST in a foreach loop so you wont have to type each one out. I also excluded it from reading the posted info for the submit button and user name field.

Code: Select all

 
if(isset($_POST['submit']))
{
 
foreach($_POST as $name => $data)
    {
            $count = 1; 
        if($name != "name" && $name != "submit"){
              
               //do your msyql query against the correct answers here
               if ($data = $mysqlanswers){
        echo "Question  #" . $count . "answer " . $data . " is correct. <br>";
              }else{
                 echo "Question  #" . $count . "answer " . $data . " is incorrect. The correct answer is" . $mysqlanswers . "<br>";
               }
              $count++
        }
    }
}
 
Now the code above is a rough draft and of course would need to be tweaked to fit your page/variables but that should give you an idea of what you can do to accomplish your task.

Hope that helps