Need Beginner PHP Project Help with writing code!

I’m in a beginner PHP class and need help with a project. We need to create a distribution table in which we collect as many student grades as the user enters and then they put it into a table. I know we have to use some sort of explode/implode function but I can’t get it to work, I feel like I just have the basics down. I have also set up an HTML page which is why I am using the “POST”. This is what I have so far…

[php]

<?php echo ""; //create first row with quantity and total price as headers echo ""; echo ""; echo ""; echo ""; } elseif ($grade>="60" && ($grade<"70")){ print "You have a D"; echo ""; } elseif ($grade>="70" && ($grade<"80")){ print "You have a C"; echo ""; } elseif ($grade>=80 && ($grade<"90")){ print "You have a B"; echo ""; } else { print "You have an A"; echo ""; } echo "
"; echo "
Student ID Score Letter Grade
"; $StudentID="$_POST[user]"; $ID=explode(",",$StudentID); echo implode(",",$ID); echo " "; $Score="$_POST[scores]"; echo $Score; echo " "; $grade=$Score; if($grade>="0" && $grade<"60"){ print "You have a F"; echo "
" [/php] We then need another table on the same page that shows the frequency of each letter grade (the mode). Lastly, we have to display the mean, maximum, and minimum scores and their corresponding letter grades. Any help is greatly appreciated!! Thanks, CourtneyK

I will give you some tips…

[php]$StudentID="$_POST[user]";[/php]

You do not need to use quotes around the post variable. If this POST variable was an integer you would convert it to a string by having those quotes. It is also common practice to use single quotes for array keys (although not always necessary). Also, you should always validate that a post variable exists before using it - otherwise it will issue ‘Undefined index’ errors. You can do this with the ternary operator or a simple if statement (which both function the same).

[php]
// example validation with if statement
// Note: your default value should match the variable type you
// expect $StudentID to be, in this case an integer 0
$StudentID = 0;
if (isset($_POST[‘user’])) {
$StudentID = $_POST[‘user’];
}

// example validation with ternary operator
$StudentID = (isset($_POST[‘user’]) ? $_POST[‘user’] : 0);

// do the same thing with $Score
[/php]

What purpose does this serve?

[php]$grade=$Score;[/php]

In your comparisons:

[php]if($grade>=“0” && $grade<“60”){[/php]

If $Score/$grade is expected to be an integer, why are you comparing it to a string? By putting quotes around your integers you are converting them to strings. You should remove the quotes:

[php]if($grade >= 0 && $grade < 60){[/php]

One more thing, why are you mixing print() and echo()? They are the same function, pick one :slight_smile:

Now, where is your form?

Have to correct myself here. There is a slight difference. echo() allows multiple strings (comma separated) to be output while print() doesn’t. e.g.

[php]echo $a, $b, $c;[/php]

Why are you exploding the student ids and then impoding them again? Neither one is being echoed. Having them in there isn’t serving any purpose that i can see.

Sponsor our Newsletter | Privacy Policy | Terms of Service