Jump to content

Recommended Posts

does anyone know how to make it so that you can make user input 7 grades and ouput average and drop the 2 lowest scores?

This is all i know how to do

 

<form method='post' action=''>

Enter Student's Name:
<br/> <input type="text" name="num1" value = " " />  
<br/>
<br/>
Enter Seven Tests Scores Here: 
<br/><input type="text" name="num1" value = "<?=$_POST['Steve']?> " />  
<br/><input type="text" name="num1" value = "<?=$_POST['Steve']?> " />  
<br/><input type="text" name="num1" value = "<?=$_POST['Steve']?> " />  
<br/><input type="text" name="num1" value = "<?=$_POST['Steve']?> " />  
<br/><input type="text" name="num1" value = " <?=$_POST['Steve']?>" />  
<br/><input type="text" name="num1" value = "<?=$_POST['Steve']?> " />  
<br/><input type="text" name="num1" value = " " />  

<br/><input type='Submit' name='Submit' value="Submit" />
</form>

<?


$steve1=$_POST['Steve'];
$a=array($_POST['Steve']);
print_r (array_sum($a)) ;













?>

Link to comment
https://forums.phpfreaks.com/topic/181302-solved-calculating-array/
Share on other sites

function getAverage($array){
//get lowest values, and counter
$counter = 0;
$lowest = 9999;//arbitrarily high values
$secondLowest = 1000;
foreach($array as $arr){
  $counter += $arr;
  if ($arr < $secondLowest){
    $secondLowest = $arr;
    if ($secondLowest < $lowest){
      $lowest = secondLowest;
    }
  }
}//end foreach
$counter -= ($lowest + $secondLowest);//drop 2 lowest
return $counter / (count($array) - 2);//return average
}//end func

 

untested, so logic might be slightly off, but i think thats about right

 

Something like this would also work:

 

<?php
function getAverage($arr)
{
asort($arr); array_shift($arr); array_shift($arr);
return array_sum($arr) / count($arr);
}

 

To use it in your form you'd make all the text inputs named 'num[]', then do something like:

 

echo getAverage($_POST['num']);

Oh wait it's saying Warning: asort() expects parameter 1 to be array, null given in

 

/Applications/XAMPP/xamppfiles/htdocs/webalizer/av.php on line 25

 

Warning: array_shift() expects parameter 1 to be array, null given in /Applications/XAMPP/xamppfiles/htdocs/webalizer/av.php on line 25

 

Warning: array_shift() expects parameter 1 to be array, null given in /Applications/XAMPP/xamppfiles/htdocs/webalizer/av.php on line 25

 

Warning: array_sum() expects parameter 1 to be array, null given in /Applications/XAMPP/xamppfiles/htdocs/webalizer/av.php on line 26

 

Warning: Division by zero in /Applications/XAMPP/xamppfiles/htdocs/webalizer/av.php on line 26

 

<form method='post' action=''>

Enter Student's Name:
<br/> <input type="text" name="num1" value = " " /> 
<br/>
<br/>
Enter Seven Tests Scores Here:
<br/><input type="text" name="num1" value = "<?=$_POST['num']?> " /> 
<br/><input type="text" name="num1" value = "<?=$_POST['num']?> " /> 
<br/><input type="text" name="num1" value = "<?=$_POST['num']?> " /> 
<br/><input type="text" name="num1" value = "<?=$_POST['num']?> " /> 
<br/><input type="text" name="num1" value = " <?=$_POST['num']?>" /> 
<br/><input type="text" name="num1" value = "<?=$_POST['num']?> " /> 
<br/><input type="text" name="num1" value = "<?=$_POST['num']?> " /> 

<br/><input type='Submit' name='Submit' value="Submit" />
</form>

<?

$arr=array($_POST['num']);

function getAverage($arr)
{
asort($arr); array_shift($arr); array_shift($arr);
return array_sum($arr) / count($arr);
}



echo getAverage($_POST['num']);



?>[/Code]

 

You didn't listen to what I said before.

To use it in your form you'd make all the text inputs named 'num[]', then do something like:

You can leave the values that you have set if you want (although, if you have error reporting high enough those will throw and undefined index warning, so ideally you should use isset() for those as well), but you might want to remove some of the spaces you have in there.

I'm sorry but I still don't really get what you mine by putting a blank [] after the num. I tried it once and it gave me even more errors. The most recent code I have doesn't say any errors when I click submit, but it returns zero.

 

<form method='post' action=''>

Enter Student's Name:
<br/> <input type="text" name="num1" value = " " />  
<br/>
<br/>
Enter Seven Tests Scores Here: 
<br/><input type="text" name="num1" value = "<?=$_POST['num']?>" />  
<br/><input type="text" name="num1" value = "<?=$_POST['num']?>" />  
<br/><input type="text" name="num1" value = "<?=$_POST['num']?>" />  
<br/><input type="text" name="num1" value = "<?=$_POST['num']?>" />  
<br/><input type="text" name="num1" value = "<?=$_POST['num']?>" />  
<br/><input type="text" name="num1" value = "<?=$_POST['num']?>" />  
<br/><input type="text" name="num1" value = "<?=$_POST['num']?>" />  

<br/><input type='Submit' name='Submit' value="Submit" />
</form>

<?

$arr=array($_POST['num']);

function getAverage($arr)
{
asort($arr); array_shift($arr); array_shift($arr);
return array_sum($arr) / count($arr);
}


if(isset($_POST['Submit']))
{
echo array_sum($arr) / count($arr);

}

?>

You put num[] so that it can be an array.. You were also creating an array with one element which was an array, which is why you were getting 0. Here:

 

<form method='post' action=''>

Enter Student's Name:
<br/> <input type="text" name="num1" value = " " />  
<br/>
<br/>
Enter Seven Tests Scores Here: 
<br/><input type="text" name="num[]" value = "<?=$_POST['num'][0]?>" />  
<br/><input type="text" name="num[]" value = "<?=$_POST['num'][1]?>" />  
<br/><input type="text" name="num[]" value = "<?=$_POST['num'][2]?>" />  
<br/><input type="text" name="num[]" value = "<?=$_POST['num'][3]?>" />  
<br/><input type="text" name="num[]" value = "<?=$_POST['num'][4]?>" />  
<br/><input type="text" name="num[]" value = "<?=$_POST['num'][5]?>" />  
<br/><input type="text" name="num[]" value = "<?=$_POST['num'][6]?>" />  
<br/><input type='Submit' name='Submit' value="Submit" />
</form>

<?
$arr = $_POST['num'];

function getAverage($arr)
{
asort($arr); array_shift($arr); array_shift($arr);
return array_sum($arr) / count($arr);
}


if(isset($_POST['Submit']))
{
echo array_sum($arr) / count($arr);
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.