PDA

View Full Version : php quiz help - urgent


king1102
07-08-2008, 10:52 PM
i m in urgnt trouble.

plz can someone rectify the following code. i need it tomorrow. this is a form actually. i m a big n00b

a billion thanx

save this as quiz01.php


<?php
$qid = "Quiz 01 - Physics";
?>
<html>
<head>
<title><? echo "$qid"; ?></title>
</head>
<body>
<p align = "center"><h3><? echo "$qid"; ?></h3></p>
<form action="results01.php" method="post">
<p align="center">Your name <input name="name" type="text" size="50">
</p>
<p>1) What happens to an ice cube when kept in water?<br>
<input type="radio" name="q1" value="Option1">
Sinks
<br>
<input type="radio" name="q1" value="Ans">
Floats
<br>
<input type="radio" name="q1" value="Option3">
Melts
<br>
<input type="radio" name="q1" value="Option4">
Cannot be decided
<br>
</p>
<p>2) Mirrors used in vehicle headlights are<br>
<input type="radio" name="q2" value="Option1">
Conacave
<br>
<input type="radio" name="q2" value="Ans">
Convex
<br>
<input type="radio" name="q2" value="Option3">
Plane<br>
<input type="radio" name="q2" value="Option4">
Any spherical mirror
<br>
</p>
<p>3) Which image can be obtained on a screen?<br>
<input type="radio" name="q3" value="Option1">
Real and erect
<br>
<input type="radio" name="q3" value="Option2">
Virtual and Inverted
<br>
<input type="radio" name="q3" value="Ans">
Real and inverted
<br>
<input type="radio" name="q3" value="Option4">
Virtual and erect<br>
</p>
<p>4) When does Snell's law fail?<br>
<input type="radio" name="q4" value="Ans">
Angle i = 0
<br>
<input type="radio" name="q4" value="Option2">
Angle i &lt; 45
<br>
<input type="radio" name="q4" value="Option3">
Angle &gt; 45
<br>
<input type="radio" name="q4" value="Option4">
Angle i = 0.<br>
</p>
<p>5) Where will the image be formed when an object is placed at the

focus of a convex mirror?<br>
<input type="radio" name="q5" value="Option1">
on Focus
<br>
<input type="radio" name="q5" value="Option2">
Between F and 2F
<br>
<input type="radio" name="q5" value="Option3">
Beyond 2F
<br>
<input type="radio" name="q5" value="Ans">
Infinity
<br>
</p>
<p>6) The colour of an object is determined by<br>
<input type="radio" name="q6" value="Option1">
The colour of light reflected by it
<br>
<input type="radio" name="q6" value="Ans">
The colour of light absorbed by it<br>
<input type="radio" name="q6" value="Option3">
The colour of light incidented on it
<br>
<input type="radio" name="q6" value="Option4">
None of the above
<br>
</p>
<p>7) Laws of reflection are applcable to<br>
<input type="radio" name="q7" value="Option1">
Plane surfaces<br>
<input type="radio" name="q7" value="Option2">
Spherical mirrors
<br>
<input type="radio" name="q7" value="Ans">
All types of reflecting surfaces
<br>
<input type="radio" name="q7" value="Option4">
Only spherical and plane
<br>
</p>
<p>8) Mirror formula is expressed as<br>
<input type="radio" name="q8" value="Option1">
1/u - 1/v = 1/f
<br>
<input type="radio" name="q8" value="Option2">
u + v = f
<br>
<input type="radio" name="q8" value="Option3">
1/v - 1/u = 1/f
<br>
<input type="radio" name="q8" value="Ans">
1/v + 1/u = 1/f
<br>
</p>
<p>9) Convex lenses are also known as<br>
<input type="radio" name="q9" value="Ans">
Converging lenses
<br>
<input type="radio" name="q9" value="Option2">
Diverging lenses
<br>
<input type="radio" name="q9" value="Option3">
Circular lenses
<br>
<input type="radio" name="q9" value="Option4">
None
<br>
</p>
<p>10) Combined power (P) of two lenses is denoted as<br>
<input type="radio" name="q10" value="Option1">
P1 - P2 = P
<br>
<input type="radio" name="q10" value="Option2">
P1 * P2 = P
<br>
<input type="radio" name="q10" value="Ans">
P1 + P/2 = P
<br>
<input type="radio" name="q10" value="Option4">
P1 / P2 = P
<br>
</p>
<p><input type="submit" name="submit" value="Submit_Quiz"></p>
<input type="hidden" name="qp" value="quiz01.php">
</form>
</body>
</html>Save this as results01.php

<?php
$title = "Quiz Results";
echo "<title>$title</title>";
// Getting the answers - Assigning the value of variables of previous page to the variables of this page //
if (isset ($_POST['submit'])) {
$name = $_POST['name'];
$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
$q4 = $_POST['q4'];
$q5 = $_POST['q5'];
$q6 = $_POST['q6'];
$q7 = $_POST['q7'];
$q8 = $_POST['q8'];
$q9 = $_POST['q9'];
$q10 = $_POST['q10'];
$qp = $_POST['qp'];
}
// Checks whether you missed an answer //
if ($name == "") {
die ("You forgot to enter your name. Go back and enter it.");
}
if ($q1 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q2 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q3 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q4 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q5 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q6 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q7 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q8 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q9 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q10 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
// Below is where the answers are actually displayed //
{
echo<<<EOT
<p>Results of $name<br><br><br>
1) $q1<br>
2) $q2<br>
3) $q3<br>
4) $q4<br>
5) $q5<br>
6) $q6<br>
7) $q7<br>
8) $q8<br>
9) $q9<br>
10) $q10<br>
</p>
EOT;
}


if ($q1 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q1 != "Ans") {
echo "Your answer was wrong. The correct answer was : Floats. <br>";
}

if ($q2 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q2 != "Ans") {
echo "Your answer was wrong. The correct answer was : Convex mirrors. <br>";
}

if ($q3 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q3 != "Ans") {
echo "Your answer was wrong. The correct answer was : Real and Inverted. <br>";
}

if ($q4 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q4 != "Ans") {
echo "Your answer was wrong. The correct answer was : when Angle i = 0. <br>";
}

if ($q5 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q5 != "Ans") {
echo "Your answer was wrong. The correct answer was : Infintiy. <br>";
}

if ($q6 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q6 != "Ans") {
echo "Your answer was wrong. The correct answer was : the colour of light absorbed by it. <br>";
}

if ($q7 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q7 != "Ans") {
echo "Your answer was wrong. The correct answer was : All types of reflection surfaces. <br>";
}

if ($q8 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q8 != "Ans") {
echo "Your answer was wrong. The correct answer was : 1/v + 1/u = 1/f. <br>";
}

if ($q9 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q1 != "Ans") {
echo "Your answer was wrong. The correct answer was : Converging lenses. <br>";
}

if ($q10 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q10 != "Ans") {
echo "Your answer was wrong. The correct answer was : P1 + P/2 = P. <br>";
}

<p><a href="$qp">Go Back To Quiz?</a>

?>can someone tell me why it is not workin.

plz: i dont no ABC abt php. i have to submit it tomorrow.

if some1 with gr8 knowldge of PHP can devote his 15minutes, my problem will be solved and thanx to all who even bothered to looka at it..:)

amitava82
08-08-2008, 05:14 AM
quiz01.php

<?php
$qid = "Quiz 01 - Physics";
?>
<html>
<head>
<title><?php echo "$qid"; ?></title>
</head>
<body>
<p align = "center"><h3><?php echo "$qid"; ?></h3></p>
<form action="results01.php" method="post">
<p align="center">Your name <input name="name" type="text" size="50">
</p>
<p>1) What happens to an ice cube when kept in water?<br>
<input type="radio" name="q1" value="Option1">
Sinks
<br>
<input type="radio" name="q1" value="Ans">
Floats
<br>
<input type="radio" name="q1" value="Option3">
Melts
<br>
<input type="radio" name="q1" value="Option4">
Cannot be decided
<br>
</p>
<p>2) Mirrors used in vehicle headlights are<br>
<input type="radio" name="q2" value="Option1">
Conacave
<br>
<input type="radio" name="q2" value="Ans">
Convex
<br>
<input type="radio" name="q2" value="Option3">
Plane<br>
<input type="radio" name="q2" value="Option4">
Any spherical mirror
<br>
</p>
<p>3) Which image can be obtained on a screen?<br>
<input type="radio" name="q3" value="Option1">
Real and erect
<br>
<input type="radio" name="q3" value="Option2">
Virtual and Inverted
<br>
<input type="radio" name="q3" value="Ans">
Real and inverted
<br>
<input type="radio" name="q3" value="Option4">
Virtual and erect<br>
</p>
<p>4) When does Snell's law fail?<br>
<input type="radio" name="q4" value="Ans">
Angle i = 0
<br>
<input type="radio" name="q4" value="Option2">
Angle i &lt; 45
<br>
<input type="radio" name="q4" value="Option3">
Angle &gt; 45
<br>
<input type="radio" name="q4" value="Option4">
Angle i = 0.<br>
</p>
<p>5) Where will the image be formed when an object is placed at the

focus of a convex mirror?<br>
<input type="radio" name="q5" value="Option1">
on Focus
<br>
<input type="radio" name="q5" value="Option2">
Between F and 2F
<br>
<input type="radio" name="q5" value="Option3">
Beyond 2F
<br>
<input type="radio" name="q5" value="Ans">
Infinity
<br>
</p>
<p>6) The colour of an object is determined by<br>
<input type="radio" name="q6" value="Option1">
The colour of light reflected by it
<br>
<input type="radio" name="q6" value="Ans">
The colour of light absorbed by it<br>
<input type="radio" name="q6" value="Option3">
The colour of light incidented on it
<br>
<input type="radio" name="q6" value="Option4">
None of the above
<br>
</p>
<p>7) Laws of reflection are applcable to<br>
<input type="radio" name="q7" value="Option1">
Plane surfaces<br>
<input type="radio" name="q7" value="Option2">
Spherical mirrors
<br>
<input type="radio" name="q7" value="Ans">
All types of reflecting surfaces
<br>
<input type="radio" name="q7" value="Option4">
Only spherical and plane
<br>
</p>
<p>8) Mirror formula is expressed as<br>
<input type="radio" name="q8" value="Option1">
1/u - 1/v = 1/f
<br>
<input type="radio" name="q8" value="Option2">
u + v = f
<br>
<input type="radio" name="q8" value="Option3">
1/v - 1/u = 1/f
<br>
<input type="radio" name="q8" value="Ans">
1/v + 1/u = 1/f
<br>
</p>
<p>9) Convex lenses are also known as<br>
<input type="radio" name="q9" value="Ans">
Converging lenses
<br>
<input type="radio" name="q9" value="Option2">
Diverging lenses
<br>
<input type="radio" name="q9" value="Option3">
Circular lenses
<br>
<input type="radio" name="q9" value="Option4">
None
<br>
</p>
<p>10) Combined power (P) of two lenses is denoted as<br>
<input type="radio" name="q10" value="Option1">
P1 - P2 = P
<br>
<input type="radio" name="q10" value="Option2">
P1 * P2 = P
<br>
<input type="radio" name="q10" value="Ans">
P1 + P/2 = P
<br>
<input type="radio" name="q10" value="Option4">
P1 / P2 = P
<br>
</p>
<p><input type="submit" name="submit" value="Submit_Quiz"></p>
<input type="hidden" name="qp" value="quiz01.php">
</form>
</body>
</html>

results01.php

<?php
$title = "Quiz Results";
echo "<title>$title</title>";
// Getting the answers - Assigning the value of variables of previous page to the variables of this page //
if (isset ($_POST['submit'])) {
$name = $_POST['name'];
$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
$q4 = $_POST['q4'];
$q5 = $_POST['q5'];
$q6 = $_POST['q6'];
$q7 = $_POST['q7'];
$q8 = $_POST['q8'];
$q9 = $_POST['q9'];
$q10 = $_POST['q10'];
$qp = $_POST['qp'];
}
// Checks whether you missed an answer //
if ($name == "") {
die ("You forgot to enter your name. Go back and enter it.");
}
if ($q1 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q2 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q3 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q4 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q5 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q6 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q7 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q8 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q9 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q10 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
// Below is where the answers are actually displayed //
{
echo<<<EOT
<p>Results of $name<br><br><br>
1) $q1<br>
2) $q2<br>
3) $q3<br>
4) $q4<br>
5) $q5<br>
6) $q6<br>
7) $q7<br>
8) $q8<br>
9) $q9<br>
10) $q10<br>
</p>
EOT;
}


if ($q1 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q1 != "Ans") {
echo "Your answer was wrong. The correct answer was : Floats. <br>";
}

if ($q2 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q2 != "Ans") {
echo "Your answer was wrong. The correct answer was : Convex mirrors. <br>";
}

if ($q3 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q3 != "Ans") {
echo "Your answer was wrong. The correct answer was : Real and Inverted. <br>";
}

if ($q4 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q4 != "Ans") {
echo "Your answer was wrong. The correct answer was : when Angle i = 0. <br>";
}

if ($q5 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q5 != "Ans") {
echo "Your answer was wrong. The correct answer was : Infintiy. <br>";
}

if ($q6 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q6 != "Ans") {
echo "Your answer was wrong. The correct answer was : the colour of light absorbed by it. <br>";
}

if ($q7 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q7 != "Ans") {
echo "Your answer was wrong. The correct answer was : All types of reflection surfaces. <br>";
}

if ($q8 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q8 != "Ans") {
echo "Your answer was wrong. The correct answer was : 1/v + 1/u = 1/f. <br>";
}

if ($q9 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q1 != "Ans") {
echo "Your answer was wrong. The correct answer was : Converging lenses. <br>";
}

if ($q10 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q10 != "Ans") {
echo "Your answer was wrong. The correct answer was : P1 + P/2 = P. <br>";
}
?>
<p><a href="<?php echo $qp; ?>">Go Back To Quiz?</a>

From where did you copy this? and why do you need this?

gagandeep
08-08-2008, 03:46 PM
I'm also learning PHP......And I think this may also work.


<?php
$title = "Quiz Results";
echo "<title>$title</title>";
// Getting the answers - Assigning the value of variables of previous page to the variables of this page //
if (isset ($_POST['submit'])) {
$name = $_POST['name'];
$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
$q4 = $_POST['q4'];
$q5 = $_POST['q5'];
$q6 = $_POST['q6'];
$q7 = $_POST['q7'];
$q8 = $_POST['q8'];
$q9 = $_POST['q9'];
$q10 = $_POST['q10'];
$qp = $_POST['qp'];
}
// Checks whether you missed an answer //
if ($name == "") {
die ("You forgot to enter your name. Go back and enter it.");
}
if ($q1 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q2 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q3 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q4 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q5 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q6 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q7 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q8 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q9 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
if ($q10 == "") {
die ("You skipped a question. Go back and give an answer for it.");
}
// Below is where the answers are actually displayed //
{
echo<<<EOT
<p>Results of $name<br><br><br>
1) $q1<br>
2) $q2<br>
3) $q3<br>
4) $q4<br>
5) $q5<br>
6) $q6<br>
7) $q7<br>
8) $q8<br>
9) $q9<br>
10) $q10<br>
</p>
EOT;
}


if ($q1 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q1 != "Ans") {
echo "Your answer was wrong. The correct answer was : Floats. <br>";
}

if ($q2 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q2 != "Ans") {
echo "Your answer was wrong. The correct answer was : Convex mirrors. <br>";
}

if ($q3 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q3 != "Ans") {
echo "Your answer was wrong. The correct answer was : Real and Inverted. <br>";
}

if ($q4 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q4 != "Ans") {
echo "Your answer was wrong. The correct answer was : when Angle i = 0. <br>";
}

if ($q5 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q5 != "Ans") {
echo "Your answer was wrong. The correct answer was : Infintiy. <br>";
}

if ($q6 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q6 != "Ans") {
echo "Your answer was wrong. The correct answer was : the colour of light absorbed by it. <br>";
}

if ($q7 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q7 != "Ans") {
echo "Your answer was wrong. The correct answer was : All types of reflection surfaces. <br>";
}

if ($q8 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q8 != "Ans") {
echo "Your answer was wrong. The correct answer was : 1/v + 1/u = 1/f. <br>";
}

if ($q9 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q1 != "Ans") {
echo "Your answer was wrong. The correct answer was : Converging lenses. <br>";
}

if ($q10 == "Ans") {
echo "You answer was correct. <br>";
}
if ($q10 != "Ans") {
echo "Your answer was wrong. The correct answer was : P1 + P/2 = P. <br>";
}

echo "<p><a href=\"$qp\">Go Back To Quiz?</a>";

?>

QwertyManiac
08-08-2008, 08:22 PM
Damn you redundant and inefficient code, you made me learn PHP functions, loops and arrays just for fixing the code to look and work slightly better. I don't know PHP but this is way better, logically and functionally.

results01.php
<?php
$title = "Quiz Results";
echo "<title>$title</title>";
// Getting the answers - Assigning the value of variables of previous page to the variables of this page //
if (isset ($_POST['submit'])) {
$name = $_POST['name'];
$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
$q4 = $_POST['q4'];
$q5 = $_POST['q5'];
$q6 = $_POST['q6'];
$q7 = $_POST['q7'];
$q8 = $_POST['q8'];
$q9 = $_POST['q9'];
$q10 = $_POST['q10'];
$qp = $_POST['qp'];
}
// Checks whether you missed an answer //
if ($name == "") {
die ("You forgot to enter your name. Go back and enter it.");
}
if (strlen($q1.$q2.$q3.$q4.$q5.$q6.$q7.$q8.$q9.$q10) < 30) {
die ("You skipped a question. Go back and give an answer for it.");
}
// Below is where the answers are actually displayed //
{
echo<<<EOT
<p>Results of $name<br><br><br>
1) $q1<br>
2) $q2<br>
3) $q3<br>
4) $q4<br>
5) $q5<br>
6) $q6<br>
7) $q7<br>
8) $q8<br>
9) $q9<br>
10) $q10<br>
</p>
EOT;
}
$questions = array( $q1, $q2, $q3, $q4, $q5, $q6, $q7, $q8, $q9, $q10 );
$answers = array( 'Floats', 'Convex mirrors', 'Real and inverted', 'when Angle i = 0', 'Infinity',
'the colour of light absorbed by it.', 'All types of reflection surfaces.',
'1/v + 1/u = 1/f', 'Converging lenses', 'P1 + P/2 = P.' );
function check($question, $i)
{
global $answers;
if ($question == "Ans") {
echo "You answer was correct. <br>";
} else {
echo "Your answer was wrong. The correct answer was : $answers[$i]. <br>";
}
}
for($i=0;$i<10;$i++) {
check($questions[$i],$i);
}
?>
<p><a href="<?php echo $qp; ?>">Go Back To Quiz?</a>

T159
08-08-2008, 08:26 PM
optimizations unto death

QwertyManiac
08-08-2008, 08:55 PM
Hello one-line quoter :p

Wasn't optimization exactly, but it was far less redundant than the original :p

king1102
10-08-2008, 06:02 PM
thx to all who helped.

Qwertymaniac: i m not into arrays and loop at the moment.

T159
10-08-2008, 06:15 PM
^^Imma chargin ur laser ?

anilmail17
10-08-2008, 06:32 PM
totally rubish code....anyone who is appearing for this test can find the answers for each question in page source code. My suggestion for the code will be to create a table with columns
1) Question
2) OptionA
3) OptionB
4) OptionC
5) OptionD
6) OptionE
7) Answer
8) QuestionID
9) Level (optional if want to have a level Master table also)

Fetch each question from the table and on submiting form check if Answer submitted by user matches with the answer for that particular QuestionID.

QwertyManiac
10-08-2008, 06:49 PM
A homework assignment and he is supposedly not "into" loops and arrays, ha!