Problem with PHP assignment
Posted: Sun May 31, 2009 6:51 pm
This is supposed to be a quiz.
Everything is working fine except for one thing.
When I complete everything, the answers that are answered correctly do not show.
for example in the list it is supposed to say:
XHTML question is: correct
but it leaves a blank space instead.
it is working fine for incorrect answers.
Also it is not adding the score for correct answers.
In Number Correct: it always says 0
and in: Your score is: _ out of 9 or _% there are always 0's in the dashes.
Incorrect is working fine correct isnt. i don't know why. i've gone through the script several times. here it is:
---
<html>
<head>
<title> Grading your Quiz - XXXXXX </title>
</head>
<body>
<?php
$num_correct = 0;
$num_total = 9;
$correct = array('excel'=>0, 'javascript'=>0, 'unix'=>0, 'xhtml'=>0, 'php'=>0, 'word'=>0, 'photoshop'=>0, 'css'=>0, 'flash'=>0);
$incorrect = array('excel'=>0, 'javascript'=>0, 'unix'=>0, 'xhtml'=>0, 'php'=>0, 'word'=>0, 'photoshop'=>0, 'css'=>0, 'flash'=>0);
//Q1 is a XHTML question.
switch ($_POST['q1']){
case '1a':
++$incorrect['xhtml'];
//echo 'you answered a <br />';
break;
case '1b':
++$num_correct['xhtml'];
//echo 'you answered b <br />';
break;
case '1c':
++$incorrect['xhtml'];
//echo 'you answered c <br />';
break;
case '1d':
++$incorrect['xhtml'];
//echo 'you answered d <br />';
break;
case '1e':
++$incorrect['xhtml'];
//echo 'you answered e <br />';
break;
default:
echo 'You forgot to answer quesion number 1. <br />';
break;
}
}
echo 'Your score is: ', $num_correct, ' out of ', $num_total, ' or ', ($num_correct/$num_total)*100 , '%';
?>
<table border='2'>
<legend> Thank you <?php echo " ", $_POST['student'], " "; ?> for taking the xxxx quiz. </legend>
<tr>
<th> Number Correct </th>
<th> Total Number </th>
<th> % </th>
</tr>
<tr>
<td> <?php echo $num_correct; ?></td>
<td> <?php echo $num_total; ?> </td>
<td> <?php echo ($num_correct/$num_total)*100; ?> </td>
</tr>
<tr>
<td colspan="3"> XHTML question is:
<?php if ($correct['xhtml']>0) {echo 'correct';}
if ($incorrect['xhtml']>0){echo 'incorrect';}
?>
</td>
</tr>
</table>
</body>
</html>
---
edit: i shortened it now
Everything is working fine except for one thing.
When I complete everything, the answers that are answered correctly do not show.
for example in the list it is supposed to say:
XHTML question is: correct
but it leaves a blank space instead.
it is working fine for incorrect answers.
Also it is not adding the score for correct answers.
In Number Correct: it always says 0
and in: Your score is: _ out of 9 or _% there are always 0's in the dashes.
Incorrect is working fine correct isnt. i don't know why. i've gone through the script several times. here it is:
---
<html>
<head>
<title> Grading your Quiz - XXXXXX </title>
</head>
<body>
<?php
$num_correct = 0;
$num_total = 9;
$correct = array('excel'=>0, 'javascript'=>0, 'unix'=>0, 'xhtml'=>0, 'php'=>0, 'word'=>0, 'photoshop'=>0, 'css'=>0, 'flash'=>0);
$incorrect = array('excel'=>0, 'javascript'=>0, 'unix'=>0, 'xhtml'=>0, 'php'=>0, 'word'=>0, 'photoshop'=>0, 'css'=>0, 'flash'=>0);
//Q1 is a XHTML question.
switch ($_POST['q1']){
case '1a':
++$incorrect['xhtml'];
//echo 'you answered a <br />';
break;
case '1b':
++$num_correct['xhtml'];
//echo 'you answered b <br />';
break;
case '1c':
++$incorrect['xhtml'];
//echo 'you answered c <br />';
break;
case '1d':
++$incorrect['xhtml'];
//echo 'you answered d <br />';
break;
case '1e':
++$incorrect['xhtml'];
//echo 'you answered e <br />';
break;
default:
echo 'You forgot to answer quesion number 1. <br />';
break;
}
}
echo 'Your score is: ', $num_correct, ' out of ', $num_total, ' or ', ($num_correct/$num_total)*100 , '%';
?>
<table border='2'>
<legend> Thank you <?php echo " ", $_POST['student'], " "; ?> for taking the xxxx quiz. </legend>
<tr>
<th> Number Correct </th>
<th> Total Number </th>
<th> % </th>
</tr>
<tr>
<td> <?php echo $num_correct; ?></td>
<td> <?php echo $num_total; ?> </td>
<td> <?php echo ($num_correct/$num_total)*100; ?> </td>
</tr>
<tr>
<td colspan="3"> XHTML question is:
<?php if ($correct['xhtml']>0) {echo 'correct';}
if ($incorrect['xhtml']>0){echo 'incorrect';}
?>
</td>
</tr>
</table>
</body>
</html>
---
edit: i shortened it now