I'm working on a PHP grade calculator, and I have a little problem.

I need for people to be able to enter numbers into several fields, with the option of leaving some fields blank. I then need to be able to divide the sum of the entered values by the number of values entered (to get averages).

I thought I had the code filled out but apparently there is a problem. I'm getting messages about problems with dividing by zero, which obviously shouldn't happen.

Here's a link to the file:
http://cdc.frontiernetcom.com/PHP/gradecalc.php

Thanks in advance.

    We'd need to see the code that's relevant to this problem...

    Diego

      Sorry, I forgot you couldn't get at the calculation code.

      It wont let me attach it so here it is pasted:


      <html>
      <head>
      <title>Grade</title>
      </head>
      <body>
      <?php
      //Code for tests
      $testcount=0;
      if ($testg1 {1}) { $testcount ++; }
      if ($testg2 {1}) { $testcount ++; }
      if ($testg3 {1}) { $testcount ++; }
      if ($testg4 {1}) { $testcount ++; }
      if ($testg5 {1}) { $testcount ++; }
      if ($testg6 {1}) { $testcount ++; }

      //Code for quizzes
      $quizcount=0;
      if ($quizg1 {1}) { $quizcount ++; }
      if ($quizg2 {1}) { $quizcount ++; }
      if ($quizg3 {1}) { $quizcount ++; }
      if ($quizg4 {1}) { $quizcount ++; }
      if ($quizg5 {1}) { $quizcount ++; }
      if ($quizg6 {1}) { $quizcount ++; }

      //Code for vocab tests
      $vocabcount=0;
      if ($vocabg1 {1}) { $vocabcount ++; }
      if ($vocabg2 {1}) { $vocabcount ++; }
      if ($vocabg3 {1}) { $vocabcount ++; }
      if ($vocabg4 {1}) { $vocabcount ++; }
      if ($vocabg5 {1}) { $vocabcount ++; }
      if ($vocabg6 {1}) { $vocabcount ++; }

      //Code for homework
      $hmwkcount=0;
      if ($hmwkg1 {1}) { $hmwkcount ++; }
      if ($hmwkg2 {1}) { $hmwkcount ++; }
      if ($hmwkg3 {1}) { $hmwkcount ++; }
      if ($hmwkg4 {1}) { $hmwkcount ++; }
      if ($hmwkg5 {1}) { $hmwkcount ++; }
      if ($hmwkg6 {1}) { $hmwkcount ++; }

      //Code for journals
      $journcount=0;
      if ($journg1 {1}) { $journcount ++; }
      if ($journg2 {1}) { $journcount ++; }
      if ($journg3 {1}) { $journcount ++; }
      if ($journg4 {1}) { $journcount ++; }
      if ($journg5 {1}) { $journcount ++; }

      //formula
      $grade = (((($testg1 + $testg2 + $testg3 + $testg4 + $testg5 + $testg6) / $testcount) 40) + ((($quizg1 + $quizg2 + $quizg3 + $quizg4 + $quizg5 + $quizg6) / $quizcount) 20) + ((($vocabg1 + $vocabg2 + $vocabg3 + $vocabg4 + $vocabg5 + $vocabg6) / $vocabcount) 20) + ((($hmwkg1 + $hmwkg2 + $hmwkg3 + $hmwkg4 + $hmwkg5 + $hmwkg6) / $hmwkcount) 10) + ((($journg1 + $journg2 + $journg3 + $journg4 + $journg5) / $journcount) * 10)) / 100;

      ?>
      <link rel="stylesheet" type="text/css" href="http://cdc.frontiernetcom.com/PHP/style.css" />

      <h3>Your grade is: <?= "$grade" ?><br><br>

      <h4>

      <?php

      if($grade >= 92.50){
      echo "You have an A! Congrats to you.";
      }
      if($grade >= 84.50 && $grade < 92.50){
      echo "You have a B. Very..uh..respectable. Thats it. Respectable.";
      }
      if($grade >= 76.50 && $grade < 84.50){
      echo "You have a C. Not so hot, killer.";
      }
      if($grade >= 69.50 && $grade < 76.50){
      echo "Ouch. You have a D. Dang.";
      }
      else if($grade < 69.50){
      echo "You are failing. HAHAHAHA! WE WIN!! Oh. sorry.";
      }
      ?>
      </h4>
      </body>
      </html>


      The error occurs in line 57, the first line of the actual formula.

        Are you sure your variables are getting from the form to your script? In other words, do you have register_globals on?

        Diego

          Yes they are. I'm getting a calculation and variables from the form.

          Before I started trying to make it possible to leave fields blank, it worked fine. But when the field is left blank, it divides by zero.

            I think the problem is, for example, when someone doesn't enter any marks for vocab, say. So $vocabcount is still 0. So you'd have to add further if statements to check for thatl

            Diego

              what further statemets would those be and how should i implement them? Or, where are they in the documentation?

              thank you

                Instead of having the entire formula in one line, you'll have to break it down for each section. So for each section you'd do:

                if ($vocabcount != 0) {
                // calculate vocab average
                }

                if ($anothercount != 0) {
                // calculate another average
                }
                // etc..

                and then at the end put all the averages together.

                Diego


                  if ($vocabcount != 0) {
                  // calculate vocab average

                  }

                  What does the "if" statement you posted do? I'm not familiar with the exclamation point thing.

                  If this just breaks up the code, what good will that to towards fixing the problem besides make it more manageable?

                    Diego25,
                    That seems to work. Thanks for all the helpfull tips and links. I must say, you are quite the man.

                      Write a Reply...