Help with if statement and for loop
Posted: Thu Oct 20, 2011 8:06 pm
I am new to php and having a hard time getting a table to print right. I have a for loop and inside the for loop an if statement. For some reason when I run the code it only goes through the if statement once and uses the first value it finds for the rest of the times it goes through the loop, hard to explain I guess.
Can anyone tell me what I am doing wrong? I have searched all over the place but cannot find any answers.The point of this code is to take any amount of user entered numbers and create a table with 3 columns, one with a counter, one with the entered number, and one with a letter grade.
Any help is much appreciated!
Can anyone tell me what I am doing wrong? I have searched all over the place but cannot find any answers.The point of this code is to take any amount of user entered numbers and create a table with 3 columns, one with a counter, one with the entered number, and one with a letter grade.
Any help is much appreciated!
<html>
<title>Student Grades</title>
<h7>Student Grades</h7>
<?php
$grade=$_POST["number"];
$general=explode(",", $grade);
array_unshift($general, $grade);
unset($general[0]);
$sum=array_sum($general);
$total=count($general);
echo "<table border=\"1\" align=\"left\">";
echo "<tr><th>Student ID</th>";
echo "<th>Score</th>";
echo "<th>Letter Grade</th></tr>";
for ($counter=1; $counter<=$total; $counter++){
echo "<tr><td>";
echo $counter;
echo "</td><td>";
echo $general[$counter];
echo "</td><td>";
if ($grade>=0 AND $grade<=59){
print "F";
}
elseif ($grade>=60 AND $grade<=69){
print "D";
}
elseif ($grade>=70 AND $grade<=79){
print "C";
}
elseif ($grade>=80 AND $grade<=89){
print "B";
}
else {
print "A";
}
echo "</td><tr>";
}
echo "</table><br>";
echo "The average grade is ". ($sum/$total);
print "<br>";
echo "The highest grade is ". max($general);
print "<br>";
echo "The lowest grade is ". min($general);
?>
</html>