Page 1 of 1

Help with if statement and for loop

Posted: Thu Oct 20, 2011 8:06 pm
by w0nker
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!

<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>

Re: Help with if statement and for loop

Posted: Thu Oct 20, 2011 9:11 pm
by Christopher

Code: Select all

$number = preg_replace('/[^0-9\.\,]/', '', $_POST["number"]);
$grades = explode(",", $grade);
// what is this for? array_unshift($general, $grade);
unset($grades[0]);     // removing the first grade for some reason?

$sum = array_sum($grades);
$total = count($grades);

for ($n=1; $n<=$total; ++$n) {

	if ($grades[$n] >= 90){
		echo "A";
	} elseif ($grades[$n] >= 80) {
		echo "B";
	} elseif ($grades[$n] >= 70) {
		echo "C";
	} elseif ($grades[$n] >= 60) {
		echo "D";
	} else {
		echo "F";
	}

}

Re: Help with if statement and for loop

Posted: Thu Oct 20, 2011 9:21 pm
by w0nker
Thanks so much for the reply, I got it to work!

this part
array_unshift($general, $grade);
unset($general[0]);
was the only way I could get the table to start with a number 1 instead of 0

this is what I changed to make it work
if ($general[$counter]>=0 AND $general[$counter]<=59){
$gr="F";
}
elseif ($general[$counter]>=60 AND $general[$counter]<=69){
$gr="D";
}
elseif ($general[$counter]>=70 AND $general[$counter]<=79){
$gr="C";
}
elseif ($general[$counter]>=80 AND $general[$counter]<=89){
$gr="B";
}
else {
$gr="A";
}
Thanks again!

Re: Help with if statement and for loop

Posted: Thu Oct 20, 2011 10:01 pm
by twinedev
Just my two cents on the way I would have done it, just to keep the main logic out of the presentation (yet keeping a single file):

Code: Select all

<?php
	
$aryScores = explode(',',preg_replace('/[^0-9.,]/','',$_POST['number']));
$fAvgScore = array_sum($aryScores)/count($aryScores);

function getLetterGrade($intScore) {
	if     ($intScore >= 90) { return 'A'; }
	elseif ($intScore >= 80) { return 'B'; }
	elseif ($intScore >= 70) { return 'C'; }
	elseif ($intScore >= 60) { return 'D'; }
	else                     { return 'F'; }
}

?><html>
<head>
	<title>Student Grades</title>
</head>
<body>
	<h7>Student Grades</h7>
	<table border="1" align="left">
		<tr>
			<th>Student ID</th>
			<th>Score</th>
			<th>Letter Grade</th>
		</tr>
		<?php foreach ($aryScores as $key=>$intScore): ?>
			<tr>
				<td><?php echo $key+1; ?></td>
				<td><?php echo $intScore; ?></td>
				<td><?php echo getLetterGrade($intScore); ?></td>
			</tr>
		<?php endforeach; ?>
	</table>
	<br>
	<p>The average grade is: <?php echo number_format($fAvgScore,1); ?></p>
	<p>The highest grade is: <?php echo max($aryScores); ?></p>
	<p>The lowest grade is: <?php echo min($aryScores); ?></p>
</body>
</html>
The extra spacing in the function isn't necessary, but put it to make it easier to read.

I like to keep echo statements pretty simple, either a variable, or a function acting on a variable, so I did the average calculation separate.

Just my own geekiness...

-Greg