Log in

View Full Version : Problem with code!



Daryl
09-12-2007, 10:06 PM
Can someone please help?!

I need to add a calling statement to the end of the script that calls the checkGrade() function and passes to it the value from the grade form variable. Nothing I tried works! :mad:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Letter Grades</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
function checkGrade($Grade) {
switch ($Grade) {
case "A":
echo "Your grade is excellent.";
break;
case "B":
echo "Your grade is good.";
break;
case "C":
echo "Your grade is fair.";
break;
case "D":
echo "Your are barely passing.";
break;
case "E":
echo "You failed.";
break;
default:
echo "You did not enter a valid letter grade.";
}
}

/* Is this the calling statement that is suppose to be here? It sure doesn't work. $Grade = checkGrade(); */

?>
</body>
</html>

Rockonmetal
09-12-2007, 10:21 PM
ok, put in code box and its in a function
you got to call the function... its sorta like calling your friend and telling him to do something but only when you say so, but you never say so, so he never does it... if you called the have then I don't know what it could be
to call the function do this

function checkGrade($Grade)
if that don't work then try this


function checkGrade()
if that don't work then I don't know what it is...

Daryl
09-12-2007, 10:55 PM
Thanks for your reply but it seems you are helping me "define" the function. I already did that a the beginning of the script. Now I need help to call the function and pass to it the grade form variable. Thanks in advance to anyone that can help.

Twey
09-12-2007, 10:59 PM
A function isn't necessary for this at all. PHP's built-in associative arrays can handle it quite nicely.

I don't see how you've called the function, since a variable to pass as a grade isn't defined anywhere. I suggest sending it via GET:
<?php
$gradeMessages = array(
'A' => 'Your grade is excellent.',
'B' => 'Your grade is good.',
'C' => 'Your grade is fair.',
'D' => 'You are barely passing.',
'E' => 'You failed.'
);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Grade Meanings</title>
</head>
<body>
<div>
<h1><?php echo $gradeMessages[@$_GET['grade']]; ?></h1>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<label>
Grade:
<select>
<?php foreach(array_keys($gradeMessages) as $grade) { ?>
<option
value="<?php echo $grade; ?>"
<?php if($grade === @$_GET['grade']) echo 'selected="selected"'; ?>>
<?php echo $grade; ?>
</option>
<?php } ?>
</select>
</label>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>

Rockonmetal
09-12-2007, 10:59 PM
Ok sorry I didn't get that in the first post...
Now If I'm reading your words correctly your saying either 1 of 2 things...
1. You want to make a form that does a grade check *I don't think so...*
2. You need help to call the script so that it shows *in that case do this:

function checkGrade()
That will tell the script IN the function to start! or "call the function"...

Edit: Twey posted before i did, I hope he's right if he is, then read his thing instead...

Twey
09-12-2007, 11:04 PM
2. You need help to call the script so that it shows *in that case do this:

function checkGrade()
That will tell the script IN the function to start! or "call the function"...No it won't: the function requires a parameter, which you haven't passed, and PHP functions are called simply as the function name with parameters in brackets: using the "function" keyword here is incorrect.