Jump to content

avg function


nikhar021

Recommended Posts

Method 1: Array's

 

Have the form element send to PHP in an array form say name the text element name="score[]"

 

then have a array loop in the script

 

<?php

$scores = $_POST['scores'];

$i = 0;
$total = 0;

foreach($scores as $value) {
   if(is_numeric($value)) {
      $total = $total + $value;
      $i++;
   }
}

if($i) {
   $avg = $total / $i;
}

echo("The average score is: ".$avg);

?>

Link to comment
https://forums.phpfreaks.com/topic/103144-avg-function/#findComment-528340
Share on other sites

the table structure is like

"CREATE TABLE studentcs2 (idtest int(6) NOT NULL,

                                  tenth decimal(4,2) NOT NULL,

        twelth decimal(4,2) NOT NULL,

  r1 decimal(4,2),

  r2 decimal(4,2),

  r3 decimal(4,2),

  r4 decimal(4,2),

  r5 decimal(4,2),

  r6 decimal(4,2),

  r7 decimal(4,2),

  r8 decimal(4,2),

  avg decimal(4,2) NOT NULL,

  backlogs int(2) NOT NULL,

  foreign key(idtest) REFERENCES studentcs(id))"

 

 

now once the entries r made avg can be calculated easily, but when i provide a function of updating results then it is not necessary that all the entries r made again and thus avg cannot be calculated by using values from the form.

suppose we have some avg say x now the user updates the result of only 1 semester ,then how to update the avg

Link to comment
https://forums.phpfreaks.com/topic/103144-avg-function/#findComment-528345
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.