nikhar021 Posted April 27, 2008 Share Posted April 27, 2008 the user i\p his her result of a number of exams in different coloumns i want to get avg of all these coloumn values for a row. can this be accomplished? Link to comment https://forums.phpfreaks.com/topic/103144-avg-function/ Share on other sites More sharing options...
DarkerAngel Posted April 27, 2008 Share Posted April 27, 2008 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 More sharing options...
DarkerAngel Posted April 27, 2008 Share Posted April 27, 2008 If your talking about MySQL I would have to know the table structure Link to comment https://forums.phpfreaks.com/topic/103144-avg-function/#findComment-528342 Share on other sites More sharing options...
nikhar021 Posted April 27, 2008 Author Share Posted April 27, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.