Jump to content

[SOLVED] array help


Thethug

Recommended Posts

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.

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;
?>

<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;
?>

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+)

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]);

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

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

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]);

?>

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

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);



?>

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

Archived

This topic is now archived and is closed to further replies.

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