Problem with PHP assignment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
apples
Forum Newbie
Posts: 6
Joined: Sun May 31, 2009 4:35 pm

Problem with PHP assignment

Post by apples »

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
Last edited by apples on Sun May 31, 2009 9:06 pm, edited 2 times in total.
apples
Forum Newbie
Posts: 6
Joined: Sun May 31, 2009 4:35 pm

Re: Problem with PHP assignment

Post by apples »

The HTML that links tothe PHP is as follows


---
<html>

<head>

<title> PHP Assignment -XXXXX</title>

</head>

<body>

<form action="xxxx.php" method="post">
<fieldset>
<legend> Please enter your name and year: </legend>

<table>
<tr> <td> <input type= "text" name="student" value=" " /></td> </tr>

<tr> <td> <select name="year" size="1">
<option value="none">Select Your Year: </option>
<option value="Freshman">Freshman</option>
<option value="Sophomore">Sophomore</option>
<option value="Junior">Junior</option>
<option value="Senior">Senior</option>
<option value="Grad">Grad</option>
<option value="Other">Other</option>
</select>
</td> </tr>
</table>
</fieldset>
<fieldset>
<legend> xxxxxReview Quiz - Please select the appropraite answer for each question. </legend>

<ol>
<li> What Does XHTML stand for?
<table>
<tr><td><input type="radio" name="q1" value="1a" /></td>
<td>a.</td>
<td>EXtreme HyperText Markup Language</td>
</tr>

<tr><td><input type="radio" name="q1" value="1b" /></td>
<td>b.</td>
<td>EXtensible HyperText Marking Language</td>
</tr>

<tr><td><input type="radio" name="q1" value="1c" /></td>
<td>c.</td>
<td>EXtensible HyperText Markup Language</td>
</tr>

<tr><td><input type="radio" name="q1" value="1d" /></td>
<td>d.</td>
<td>EXtreme HyperText Marking Language</td>
</tr>

<tr><td><input type="radio" name="q1" value="1e" /></td>
<td>e.</td>
<td>EXtra HyperText Markup Language</td>
</tr>
</li>

</ol>
</fieldset>

<input type="submit" value="Grade Quiz Now" name="Submit" />
<input type="reset" value="Clear Answers-Do Not Grade" name="reset" />


</form>

</body>
</html>
Last edited by apples on Sun May 31, 2009 9:06 pm, edited 2 times in total.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Problem with PHP assignment

Post by mikemike »

Can you shorten your posts a little to be more specific code-wise? I don't think many people will be bothered to go through that much code.
apples
Forum Newbie
Posts: 6
Joined: Sun May 31, 2009 4:35 pm

Re: Problem with PHP assignment

Post by apples »

I don't know what to do.

The second post is irrelevant just for reference if you need to know what the actual content of the page was.
The problem I'm facing is in the first post.
but I don't know what the problem is. or where the problem is.
as i said in teh first post, i know the problem is related to the "correct" answers, but i don't know what.

So ic an't make it specific because i don't know where the problem lies.
apples
Forum Newbie
Posts: 6
Joined: Sun May 31, 2009 4:35 pm

Re: Problem with PHP assignment

Post by apples »

Nevermind I got it.
Post Reply