Thethug Posted October 29, 2009 Share Posted October 29, 2009 Does anyone know how to require 10 text fields for individual grades and output class average? (10 pt grading scale) Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/ Share on other sites More sharing options...
ngreenwood6 Posted October 29, 2009 Share Posted October 29, 2009 I am not sure that I understand your question fully since it is pretty vague but you can create arrays from inputs. For example: Grade 1:<input name="grades[]" type="text" /> Grade 2:<input name="grades[]" type="text" /> Then in your php page you would do this: <?php //initalize the total $total = 0; //loop through the grades entered foreach($_POST['grades'] as $grade){ $total += $grade; } //show the total echo $total; ?> Basically all you are doing is taking the grade from the array and adding it to the total and returning it. If that doesnt answer your question please clarify it for me. Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-946834 Share on other sites More sharing options...
priti Posted October 29, 2009 Share Posted October 29, 2009 in addition - To Calculate average $totalGrades=count($_POST['grades']); foreach($_POST['grades'] as $grade){ $total += $grade; } $avg=$total/$totalGrades; echo 'Class Average: '.$avg; Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-946953 Share on other sites More sharing options...
Thethug Posted October 29, 2009 Author Share Posted October 29, 2009 it said Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/webalizer/average_array.php on line 14 0 on the code that greenwood gave. Is the html wrong? <form method='post' action=''> Grade 1:<input name="grades[]" type="text" /> Grade 2:<input name="grades[]" type="text" /> <input type='Submit' name='Submit' value="Submit" /> </form> <? //initalize the total $total = 0; //loop through the grades entered foreach($_POST['grades'] as $grade){ $total += $grade; } //show the total echo $total; ?> Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947190 Share on other sites More sharing options...
mpharo Posted October 29, 2009 Share Posted October 29, 2009 Your array isnt initialized until you perform the post, you need to add an if statment to see if it is posted yet, then do it else do nothing... Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947203 Share on other sites More sharing options...
Thethug Posted October 29, 2009 Author Share Posted October 29, 2009 Sorry I didn't catch the last part of what you said. Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947206 Share on other sites More sharing options...
mpharo Posted October 29, 2009 Share Posted October 29, 2009 <form method='post' action=''>Grade 1:<input name="grades[]" type="text" /> Grade 2:<input name="grades[]" type="text" /> <input type='Submit' name='Submit' value="Submit" /> </form> <?php //initalize the total $total = 0; //loop through the grades entered if ($_POST['grades']) { foreach($_POST['grades'] as $grade){ $total += $grade; } } //show the total echo $total; ?> Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947208 Share on other sites More sharing options...
Thethug Posted October 29, 2009 Author Share Posted October 29, 2009 When I try that, it won't output anything. Do I need to include the php POST tag in my html? Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947210 Share on other sites More sharing options...
mpharo Posted October 29, 2009 Share Posted October 29, 2009 I forgot to copy for beginning form element, copy and paste the above again and it will work... also to make sure you are using <?php ?> and not just <? ?>... Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947211 Share on other sites More sharing options...
Thethug Posted October 29, 2009 Author Share Posted October 29, 2009 Also, do you know how to make it so that you input 10 grades instead of 2 and it shows the total? and also tell if it's an A, B, C, D, or F? Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947361 Share on other sites More sharing options...
mikesta707 Posted October 29, 2009 Share Posted October 29, 2009 if you want 10 grades in stead of two than just add 10 input text fields instead of two. just add 10 of these <input name="grades[]" type="text" /> as far as an letter grade, you could do function getGrade($grade){ if ($grade > 89.5){//is A return 'A'; { elseif ($grade < 89.5 && $grade > 79.5){ return 'B'; } elseif($grade < 79.5 && $grade > 69.5){ return 'C'; } elseif ($grade < 69.5 && $grade > 59.5){ return 'D'; } return 'F'; } test code echo getGrade(75)."<br />"; echo getGrade(66)."<br />"; echo getGrade(82)."<br />"; echo getGrade(89.6)."<br />"; echo getGrade(99)."<br />"; echo getGrade(50)."<br />"; output: C D B A A F I don't consider + or - grades tho (like A-, B+) Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947365 Share on other sites More sharing options...
Thethug Posted October 29, 2009 Author Share Posted October 29, 2009 When I tried changing the text fields to 10 instead of 2, it spat out averages like 548 haha. I'll try your code though. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947367 Share on other sites More sharing options...
mikesta707 Posted October 29, 2009 Share Posted October 29, 2009 Then you aren't calculating the average. You are just calculating the total. you can calulate the average like so $total = 0; foreach($_POST['grades'] as $grade){ $total += $grade; } $average = $total/count($_POST['grades]); Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947368 Share on other sites More sharing options...
mpharo Posted October 29, 2009 Share Posted October 29, 2009 and also remember average grade is calculated differently than median grade... Average sum of elements / number of elements Median Step 1: Count the total numbers given. There are 5 elements or numbers in the distribution. Step 2: Arrange the numbers in ascending order. 1,2,4,5,7 Step 3: The total elements in the distribution (5) is odd. The middle position can be calculated using the formula. (n+1)/2 So the middle position is (5+1)/2 = 6/2 = 3 The number at 3rd position is = Median = 4 Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947372 Share on other sites More sharing options...
mikesta707 Posted October 29, 2009 Share Posted October 29, 2009 i took the liberty of writing a median grade function because I'm bored at work. tested and test code output is below function getMedian($scores){ sort($scores, SORT_NUMERIC); print_r($scores);//you can comment this out $count = count($scores); if ($count % 2 != 0){ $key = ceil($count/2) - 1; return $scores[$key]; } else { $key1 = $count/2 - 1; $key2 = $key1 + 1; return (($scores[$key1] + $scores[$key2])/2); } } $scores = array(12, 43, 65, 23, 84); $scores2 = array(44, 55, 23, 67, 88, 90); echo getMedian($scores)."<br />"; echo getMedian($scores2)."<br />"; output Array ( [0] => 12 [1] => 23 [2] => 43 [3] => 65 [4] => 84 ) ans: 43 Array ( [0] => 23 [1] => 44 [2] => 55 [3] => 67 [4] => 88 [5] => 90 ) ans: 61 Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947385 Share on other sites More sharing options...
Thethug Posted October 29, 2009 Author Share Posted October 29, 2009 When i try the code, it says Parse error: syntax error, unexpected T_ELSEIF in /Applications/XAMPP/xamppfiles/htdocs/webalizer/average_array.php on line 22 Line 22 would be this line: elseif ($grade < 89.5 && $grade > 79.5){ <form method='post' action=''> Grade 1:<input name="grades[]" type="text" /> Grade 2:<input name="grades[]" type="text" /> Grade 3:<input name="grades[]" type="text" /> Grade 4:<input name="grades[]" type="text" /> Grade 5:<input name="grades[]" type="text" /> Grade 6:<input name="grades[]" type="text" /> Grade 7:<input name="grades[]" type="text" /> Grade 8:<input name="grades[]" type="text" /> Grade 9:<input name="grades[]" type="text" /> Grade 10:<input name="grades[]" type="text" /> <input type='Submit' name='Submit' value="Submit" /> </form> <?php function getGrade($grade){ if ($grade > 89.5){//is A return 'A'; { elseif ($grade < 89.5 && $grade > 79.5){ return 'B'; } elseif($grade < 79.5 && $grade > 69.5){ return 'C'; } elseif ($grade < 69.5 && $grade > 59.5){ return 'D'; } return 'F'; } echo getGrade(75)."<br />"; echo getGrade(66)."<br />"; echo getGrade(82)."<br />"; echo getGrade(89.6)."<br />"; echo getGrade(99)."<br />"; echo getGrade(50)."<br />"; $total = 0; foreach($_POST['grades'] as $grade){ $total += $grade; } $average = $total/count($_POST[grades]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947400 Share on other sites More sharing options...
mikesta707 Posted October 29, 2009 Share Posted October 29, 2009 ooops simple syntax error function getGrade($grade){ if ($grade > 89.5){//is A return 'A'; }//fixed here. elseif(... etc)//etc Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947402 Share on other sites More sharing options...
Thethug Posted October 29, 2009 Author Share Posted October 29, 2009 For some reason it won't output anything and it says A A B C D F before I enter anything. I've tried to mess around with the code but no luck so far. Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947407 Share on other sites More sharing options...
mikesta707 Posted October 29, 2009 Share Posted October 29, 2009 .. well thats because you copy pasted my code instead of applying it to your situation. take out the echo getGrade(75)."<br />"; echo getGrade(66)."<br />"; echo getGrade(82)."<br />"; echo getGrade(89.6)."<br />"; echo getGrade(99)."<br />"; echo getGrade(50)."<br />"; part, and add echo getGrades($average); at the bottom after you define average Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947411 Share on other sites More sharing options...
Thethug Posted October 29, 2009 Author Share Posted October 29, 2009 It says that there is an error on line 21? It doesn't make any sense because it's only this: $total = 0; <form method='post' action=''> Grade 1:<input name="grades[]" type="text" value = "<?=$_POST['grades']?>" /> Grade 2:<input name="grades[]" type="text" value = "<?=$_POST['grades']?>" /> Grade 3:<input name="grades[]" type="text" value = "<?=$_POST['grades']?>"/> Grade 4:<input name="grades[]" type="text" value = "<?=$_POST['grades']?>"/> Grade 5:<input name="grades[]" type="text" value = "<?=$_POST['grades']?>"/> Grade 6:<input name="grades[]" type="text" value = "<?=$_POST['grades']?>"/> Grade 7:<input name="grades[]" type="text" value = "<?=$_POST['grades']?>"/> Grade 8:<input name="grades[]" type="text" value = "<?=$_POST['grades']?>"/> Grade 9:<input name="grades[]" type="text" value = "<?=$_POST['grades']?>"/> Grade 10:<input name="grades[]" type="text" value = "<?=$_POST['grades']?>"/> <input type='Submit' name='Submit' value="Submit" /> </form> <?php $grade=$_POST['grades'] $total = 0; foreach($_POST['grades'] as $grade){ $total += $grade; }s $average = $total/count($_POST["grades"]); function getGrade($grade){ if ($grade > 89.5){//is A return 'A'; } elseif ($grade < 89.5 && $grade > 79.5){ return 'B'; } elseif($grade < 79.5 && $grade > 69.5){ return 'C'; } elseif ($grade < 69.5 && $grade > 59.5){ return 'D'; } return 'F'; } echo getGrades($average); ?> Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947417 Share on other sites More sharing options...
Thethug Posted October 29, 2009 Author Share Posted October 29, 2009 Anyone know? Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947437 Share on other sites More sharing options...
mpharo Posted October 29, 2009 Share Posted October 29, 2009 there is a s after the closing loop bt that is out of place... Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947446 Share on other sites More sharing options...
Thethug Posted October 29, 2009 Author Share Posted October 29, 2009 That didn't work. It says unexpected T_VARIABLE. Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947452 Share on other sites More sharing options...
MadTechie Posted October 29, 2009 Share Posted October 29, 2009 $grade=$_POST['grades'] should be $grade=$_POST['grades']; note the ; Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947458 Share on other sites More sharing options...
Thethug Posted October 29, 2009 Author Share Posted October 29, 2009 MadTechie it brings up a whole series of problems when i do that. Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/webalizer/average_array.php on line 22 Warning: Division by zero in /Applications/XAMPP/xamppfiles/htdocs/webalizer/average_array.php on line 25 Fatal error: Call to undefined function getGrades() in /Applications/XAMPP/xamppfiles/htdocs/webalizer/average_array.php on line 48 Quote Link to comment https://forums.phpfreaks.com/topic/179449-solved-array-help/#findComment-947461 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.