<?php
//could do to change this as not good for internet exploder.
if (isset($_POST['calc']))
{
//this function will return the value of the field if it was a number, else it will return 0 and reduce the master divide fo the average.
function numCheck(&$masterDiv,$a){
if(ctype_digit($a)
{
return $a;
}
else{
--$masterDiv;
return 0;
}
//number to divide with to get the average
$masterDiv = count($_POST) - 1;//minus your input button
//running total of values
$runningTotal = 0;
//get all the keys of the post array
$keys = array_keys($_POST);
//loop through based on the count of the $_POST
for($i=0 ; $i < count($_POST) ; ++$i)
{
//as the $key array is based exactly on the $array array the key is pos $i in the array:
$theKey = $keys[$i];
//now we have the associative key we can easily get the value
$theValue = $array[$theKey];
//now do what ever you want with the data, provided the current is not the calc button
if($theKey != 'calc')
{
//using the function we built at the top this should add the value sent to the runningTotal if it is a number, else it will return a zero for the runningTotal and reduce by 1 the masterDivide meaning it will not be including when calculating the average
$runningTotal += numCheck($masterDiv,$theValue);
}
}
//now calculate the average based on the mastser divide and running total.
$ave=$runningTotal / $masterDiv;
}