Below is the code I have used for my php program which is linked to a html file, this program calculates grades, everything is correct, except for the fact that If the user selects the option "not done yet / excused", it must be able to calculate without taking the % of the whatever quzi, mid-term, etc that that they missed, ie if they have only taken 50% of the available marks in the course, it must be able to just calculate that. I have set the value of "not done yet / excused" to -1......

Could someone help me on this, any help is greatly appreciated

<html>
<head>
<link rel="stylesheet" type="text/css" href="http://96.48.39.33/student13/bala2.css" />
<title>Grade Calculator</title>
</head>
<body>
<?php
$grade=0;
if ($_POST['Quiz1'] >= 0 && $_POST['Quiz1'] <= 100)
	$grade = $grade + $_POST['Quiz1'] * 0.05;
else
  	$grade = $grade + 5; 
if ($_POST['Mid-Term'] >= 0 && ($_POST['Mid-Term'] <= 100))
	$grade = $grade + $_POST['Mid-Term'] * 0.30;
else
  	$grade = $grade + 30; 
if ($_POST['Quiz2'] >= 0 && ($_POST['Quiz2'] <= 100))
	$grade = $grade + $_POST['Quiz2'] * 0.05;
else
  	$grade = $grade + 5; 
if ($_POST['AL1'] >= 0 && ($_POST['AL1'] <= 100))
	$grade = $grade + $_POST['AL1'] * 0.05;
else
   	$grade = $grade + 5; 
if ($_POST['AL2'] >= 0 && ($_POST['AL2'] <= 100))
	$grade = $grade + $_POST['AL2'] * 0.05;
else
  	$grade = $grade + 5; 
if ($_POST['Participation'] >= 0 && ($_POST['Participation'] <= 100))
	$grade = $grade + $_POST['Participation'] * 0.10;
else
 	$grade = $grade + 10; 
if ($_POST['Final'] >= 0 && ($_POST['Final'] <= 100))
	$grade = $grade + $_POST['Final'] * 0.40;
else
  	$grade = $grade + 40;

?>
<?php
echo "Grade Report: <br />";
echo "<br />";
?>
Your final grade percentage will be:  <?php echo $grade . "%";
echo "<br />";
{
if ($grade > 90.9)
	echo "Your final grade for the course will be: A+ <br />";
elseif ($grade > 85.9)
	echo "Your final grade for the course will be: A  <br />";
elseif ($grade > 79)
	echo "Your final grade for the course will be: A-  <br />";
elseif ($grade > 76.9)
	echo "Your final grade for the course will be: B+  <br />";
elseif ($grade >71.9)
	echo "Your final grade for the course will be: B  <br />";
elseif ($grade > 69.9)
	echo "Your final grade for the course will be: B-  <br />";
elseif ($grade > 65.9)
	echo "Your final grade for the course will be: C+  <br />";
elseif ($grade > 59.9)
	echo "Your final grade for the course will be: C  <br />";
elseif ($grade > 54.9)
	echo "Your final grade for the course will be: C-  <br />";
elseif ($grade > 49.9)
	echo "Your final grade for the course will be: P  <br />";
else
	echo "Your final grade for the course will be: F  <br />";

}

?>
</body>
</html>

Thanks,
Tony

    I might approach it something like this:

    $critera = array(
       'Quiz1' => 5,
       'Mid-Term' => 30,
       'Quiz2' => 5,
       'AL1' => 5,
       'AL2' => 5,
       'Participation' => 10,
       'Final' => 40
    );
    $total = $possible = 0;
    foreach($critera as $key => $weight)
    {
       if(isset($_POST[$key]))
       {
          if($_POST[$key] >= 0 && $_POST[$key] <= 100)
          {
             $total += $_POST[$key] * $weight;
             $possible += 100 * $weight;
          }
          elseif($_POST[$key] != -1)
          {
             user_error("Invalid value '{$_POST[$key]}' for '$key'");
          }
       }
       else
       {
          // whatever you want to do if this form field is not submitted at all
       }
    }
    if($possible > 0)
    {
       $grade = $total / $possible;
    }
    else
    {
       // Whatever you want to do if no valid 0-100 grades were entered
    }
    

    Note that with this method the weight given to each grade does not have to add up to 100, the values just have to reflect whatever relative importance you want to give each grade.

      NogDog;10921136 wrote:

      I might approach it something like this:

      $critera = array(
         'Quiz1' => 5,
         'Mid-Term' => 30,
         'Quiz2' => 5,
         'AL1' => 5,
         'AL2' => 5,
         'Participation' => 10,
         'Final' => 40
      );
      $total = $possible = 0;
      foreach($critera as $key => $weight)
      {
         if(isset($_POST[$key]))
         {
            if($_POST[$key] >= 0 && $_POST[$key] <= 100)
            {
               $total += $_POST[$key] * $weight;
               $possible += 100 * $weight;
            }
            elseif($_POST[$key] != -1)
            {
               user_error("Invalid value '{$_POST[$key]}' for '$key'");
            }
         }
         else
         {
            // whatever you want to do if this form field is not submitted at all
         }
      }
      if($possible > 0)
      {
         $grade = $total / $possible;
      }
      else
      {
         // Whatever you want to do if no valid 0-100 grades were entered
      }
      

      Note that with this method the weight given to each grade does not have to add up to 100, the values just have to reflect whatever relative importance you want to give each grade.

      Hi,

      Is there any way to implement the same function using my PHP code, because I have only started using php for 3 weeks and some of the functions have not been taught before or i dun understand it, please help

        Jintu;10921143 wrote:

        Hi,

        Is there any way to implement the same function using my PHP code, because I have only started using php for 3 weeks and some of the functions have not been taught before or i dun understand it, please help

        I would hope you learned about [man]switch[/man] before functions, so hopefully not.

        After you learn about switches, check up on functions at http://us3.php.net/manual/en/functions.user-defined.php

          Write a Reply...