PDA

View Full Version : php Code: need help


Markpyro
04-28-2005, 7:52 PM
<?php

if (isset($_POST['question1']) && isset($_POST['question2']) && isset($_POST['question3']) && isset($_Post['question4'])

{
echo "You filled in all the questions.<br>\n";

if ($_POST['question1'] == "a")
{
$score++;
echo "You have answered question one correctly.<br>\n";
}
else
{
echo "You have not answered question one correctly.<br>\n";
}
if ($_POST['question2'] == "b")
{
$score++;
echo "You have answered question two correctly.<br>\n";
}
else
{
echo "You have not answered question two correctly.<br>\n";
}
if ($_POST['question3'] == "b")
{
$score++;
echo "You have answered question three correctly.<br>\n";
}
else
{
echo "You have not answered question three correctly.<br>\n";
}
if ($_POST['question4'] == "c")
{
$score++;
echo "You have answered question four correctly.<br>\n";
}
else
{
echo "You have not answered question four correctly.<br>\n";
}
if ($_POST['question5'] == "b")
{
$score++;
echo "You have answered question five correctly.<br>\n";
}
else
{
echo "You have not answered question five
correctly.<br>\n";
}
if ($_POST['question6'] == "c")
{
$score++;
echo "You have answered question six correctly.<br>\n";
}
else
{
echo "You have not answered question six correctly.<br>\n";
}
if ($_POST['question7'] == "a")
{
$score++;
echo "You have answered question seven correctly.<br>\n";
}
else
{
echo "You have not answered question seven correctly.<br>\n";
}
if ($_POST['question8'] == "c")
{
$score++;
echo "You have answered question eight correctly.<br>\n";
}
else
{
echo "You have not answered question eight correctly.<br>\n";
}
if ($_POST['question9'] == "b")
{
$score++;
echo "You have answered question nine correctly.<br>\n";
}
else
{
echo "You have not answered question nine correctly.<br>\n";
}
if ($_Post['question10'] == "a")
{
$score++;
echo "You have answered question Ten correctly. <br>\n";
}
else
{
echo "You have not answered question 10 correctly. <br>\n";
}
if ($_Post['question11'] == "c")
{
$score++;
echo "You have answered question Eleven correctly. <br>\n";
}
else
{
echo "You have not answered question Eleven correctly. <br>\n";
}
if ($_Post['question12'] == "c")
{
$score++;
echo "You have answered question Ten correctly. <br>\n";
}
else
{
echo "You have not answered question 10 correctly. <br>\n";
}



}
else
{
echo "You did not fill in all of the questions. Please press the back button and fill them.";
}


<br>

$total = "$_Post[fl]" . "" . "$score";
echo "You got a score of" . "" . "$total" . "" . "out of 12 possible";

?>

EDIT- the code didnt turn out right. The "if (isset($_POST['question1']) && isset($_POST['question2']) && isset($_POST['question3']) && isset($_Post['question4'])"
should all be on one line.


This is after you press submit on on a previous page. It gives me a parse error, saying theres something wrong with line 9.

Can anyone help?

Graeme
04-28-2005, 10:20 PM
Try setting $score = 0; right at the very top before the long if statements.

Black.Ice
04-29-2005, 12:21 AM
Mark, you had three errors in your code.

The first error is:

if (isset($_POST['question1']) && isset($_POST['question2']) && isset($_POST['question3']) && isset($_Post['question4'])


The $_POST global is not capitalized. I actually don't know if that causes a parse error, but it's good to keep it capped.

Corrected code:
if (isset($_POST['question1']) && isset($_POST['question2']) && isset($_POST['question3']) && isset($_POST['question4'])



The second major error is in:

if (isset($_POST['question1']) && isset($_POST['question2']) && isset($_POST['question3']) && isset($_Post['question4'])

You forgot to put a parenthesis to close off the if structure. You only have one parenthesis which closes the isset function.

Corrected code:
if (isset($_POST['question1']) && isset($_POST['question2']) && isset($_POST['question3']) && isset($_POST['question4']))



And the third error near the bottom is:

<br>

Corrected code

echo "<br/>";


Lastly, there are a few things you can do to improve code functionality.

It would be to set variables so they are not post variables. What I mean is, at the top of the code, put something like this:


$variableOne = $_POST["question1"];

Not only does this make it easier to write several times, it's a little bit more secure (if you have auto-globals on). However, judging by the simplicity of your script, I doubt you would need it.

Hope to help!

Jeff
04-29-2005, 4:54 AM
Bah, Radlin and Black.Ice beat me to it :) All of their suggestions are good. You should set a starting value for $score to 0.


Anyway, I would like to add that this...


$total = "$_Post[fl]" . "" . "$score";
echo "You got a score of" . "" . "$total" . "" . "out of 12 possible";

Seems to me like it would be best as...


echo "You got a score of " . $score . " out of 12 possible";

I don't think a new variable named $total is necessary. Although, I'm not sure what $_Post[fl] is for.

Oh, and the way the if statements are set up now, the score will print even if they don't answer all of the questions. You should move that to right after the question 12 section.

BSTRhino
04-29-2005, 5:38 AM
XML allows you to put your ending slash straight away after your tag, but in HTML you need to have a space just before the space. So <br /> not <br/>.

I know everyone has said it, but $score = 0 might not be necessary in every programming language, but if one of my students in my programming class missed out that line I cut your arms off. Just kidding. But I would definitely have given you a lecture that would be along the lines of: "don't assume." If you want a variable to be zero then set it to be zero. You probably want to learn that now so that by the time you reach a university programming class, someone like me doesn't scold you about that heh...

Markpyro
04-29-2005, 11:52 AM
what I wanted with total is, for the person's name (after being entered) to be presented with their score, which is what $_POST['fl] was. But I need to configure mysql, and delete that part.

Black.Ice
04-29-2005, 1:49 PM
I find it funny that all the moderators seem to have programming skills. :D

Anywyas, PHP has an auto-initialization - similar to Java. While it is not necessary, it is recommended.