kaveman50 Posted November 12, 2009 Share Posted November 12, 2009 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)) ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/ Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956423 Share on other sites More sharing options...
kaveman50 Posted November 12, 2009 Author Share Posted November 12, 2009 and then would i just do print_r($counter) and leave my posts the way they are after i add in your code? Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956428 Share on other sites More sharing options...
Alex Posted November 12, 2009 Share Posted November 12, 2009 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']); Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956430 Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 ahh wow AlexWD's solution is way better way way better. you would just echo the returned value. completely forgot about asort haha Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956433 Share on other sites More sharing options...
kaveman50 Posted November 12, 2009 Author Share Posted November 12, 2009 Thanks guys. =) Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956501 Share on other sites More sharing options...
kaveman50 Posted November 12, 2009 Author Share Posted November 12, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956514 Share on other sites More sharing options...
Alex Posted November 12, 2009 Share Posted November 12, 2009 Make sure the post was submitted: if(isset($_POST['Submit'])) { echo getAverage($_POST['num']); } isset() Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956520 Share on other sites More sharing options...
kaveman50 Posted November 13, 2009 Author Share Posted November 13, 2009 For some reason it still gives me all those warnings. The only difference now is that it appears after you press submit. Do I also need to do the <?=$=......?> for the value of my submit button? Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956522 Share on other sites More sharing options...
Alex Posted November 13, 2009 Share Posted November 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956529 Share on other sites More sharing options...
kaveman50 Posted November 13, 2009 Author Share Posted November 13, 2009 What spaces are you talking about? Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956546 Share on other sites More sharing options...
Alex Posted November 13, 2009 Share Posted November 13, 2009 Things like: <br/><input type="text" name="num1" value = "<?=$_POST['num']?> " /> Notice you have a space after <?=$_POST['num']?> before " Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956548 Share on other sites More sharing options...
mikesta707 Posted November 13, 2009 Share Posted November 13, 2009 shouldn't it be $_POST['num1'] not $_POST['num']? Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956565 Share on other sites More sharing options...
Alex Posted November 13, 2009 Share Posted November 13, 2009 shouldn't it be $_POST['num1'] not $_POST['num']? Not if kaveman50 followed my suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956567 Share on other sites More sharing options...
mikesta707 Posted November 13, 2009 Share Posted November 13, 2009 ahh thought i say a 1 in your suggestion Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-956568 Share on other sites More sharing options...
kaveman50 Posted November 14, 2009 Author Share Posted November 14, 2009 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-957574 Share on other sites More sharing options...
Alex Posted November 14, 2009 Share Posted November 14, 2009 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-957583 Share on other sites More sharing options...
kaveman50 Posted November 14, 2009 Author Share Posted November 14, 2009 It still comes up as 0 Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-957633 Share on other sites More sharing options...
Alex Posted November 14, 2009 Share Posted November 14, 2009 No it's not, I even tested it. Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-957642 Share on other sites More sharing options...
kaveman50 Posted November 14, 2009 Author Share Posted November 14, 2009 Oh I didn't see the array POST you added in the second time. My bad. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-957645 Share on other sites More sharing options...
kaveman50 Posted November 16, 2009 Author Share Posted November 16, 2009 Oh yeah how would I drop the 2 lowest scores? Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-958510 Share on other sites More sharing options...
kaveman50 Posted November 16, 2009 Author Share Posted November 16, 2009 . Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-958535 Share on other sites More sharing options...
kaveman50 Posted November 17, 2009 Author Share Posted November 17, 2009 . Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-959234 Share on other sites More sharing options...
Alex Posted November 17, 2009 Share Posted November 17, 2009 The function that I originally posted for you to use includes this functionality. Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-959237 Share on other sites More sharing options...
kaveman50 Posted November 17, 2009 Author Share Posted November 17, 2009 It just displays the average without it rounded off, though. Quote Link to comment https://forums.phpfreaks.com/topic/181302-solved-calculating-array/#findComment-959262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.