Reputation: 197
Hi guys I'm doing arrays for PHP that list all the column output and get the average of test scores but I can't seem to figure out the logic to get the average of each column. But I'm not too sure if I'm doing it right or wrong because my output for average is all 0 and I don't know how to change it to read the value and make it calculate.
Much appreciate it if you can help. Thank you.
Basically, I want the output to be like this;-
RM = Physics : 35, Maths : 30, Chemistry : 39, English : 80,
RM Average Scores: 46
Justin = Physics : 61, Maths : 10, Chemistry : 45, English : 33,
Justin Average Scores: 37.25
Miley = Physics : 25, Maths : 100, Chemistry : 88, English : 60,
Miley Average Scores: 68.25
This is my array:-
<?php
$students = array(
"RM" => array("test1" => 35, "test2" => 30,"test3" => 39, "test4" => 80),
"Justin" => array("test1" => 61, "test2" => 10,"test3" => 45, "test4" => 33),
"Miley" => array("test1" => 25, "test2" => 100,"test3" => 88, "test4" => 60),
);
?>
This is my code:-
<table style="border: 1px solid black">
<?php
echo "<td><p><b>Listing All Student Tests and Scores:-</b></p></td>";
$the_students = array_keys($tudents);
for($i = 0; $i < count($students); $i++) {
echo "<tr>";
// Output All Data
echo "<td><b>". $the_students[$i] . "</b>" . " = ";
foreach($students[$the_students[$i]] as $student => $score) {
echo "<b>". $student ."</b>". " : " . $score. ", ";
}
echo "<br>";
// Average Output
echo "<b>". $students[$i]. " Average Scores</b>: ";
if (array_key_exists($i, $the_students)) {
echo average_scores($students, $the_students);
}
echo "</td>";
echo "</tr>";
}
?>
</table>
I use function and put it at the end of my code:-
<?php
function average_scores($students, $i) {
$total = 0;
$students = array();
foreach ($students as $student => $data) {
$total += $data[$i];
}
return $total / 4;
}
?>
Upvotes: 0
Views: 241
Reputation: 1
Try foreach
to loop through students get the name which is the key and get also the student which is associative array, get only the values grades of student and destruct them into variables, printf
to print them out in more readable format.
echo '<table style="border: 1px solid black">';
echo "<tr><th>Listing All Student Tests and Scores:-</th></tr>";
foreach ($students as $name => $student) {
$grades = array_values($student);
[$Physics, $Maths, $Chemistry, $English] = $grades;
echo "<tr><td>";
printf("%s = Physics : %d, Maths : %d, Chemistry : %d, English : %d<br>
%s Average Scores: %.2f"
,$name, $Physics, $Maths, $Chemistry, $English
,$name, average_scores($grades));
echo "</td></tr>";
}
echo "</table>";
For average you can use array_sum
to sum all grades and divide them by their count.
function average_scores($grades) {
return array_sum($grades)/count($grades);
}
Upvotes: 1
Reputation: 738
Since you are already looping, try like this
<table style="border: 1px solid black">
<?php
echo "<td><p><b>Listing All Student Tests and Scores:-</b></p></td>";
$the_students = array_keys($tudents);
for($i = 0; $i < count($students); $i++) {
$total = 0;
echo "<tr>";
// Output All Data
echo "<td><b>". $the_students[$i] . "</b>" . " = ";
foreach($students[$the_students[$i]] as $student => $score) {
$total += $score;
echo "<b>". $student ."</b>". " : " . $score. ", ";
}
echo "<br>";
// Average Output
echo "<b>". $students[$i]. " Average Scores</b>: ";
echo $total/ count($students[$the_students[$i]]);
echo "</td>";
echo "</tr>";
}
?>
</table>
The best practice would be to keep HTML and PHP code separate. Use the PHP marking whenever needed, it will improve the code readability EG:
echo "<b>". $students[$i]. " Average Scores</b>: ";
// convert to
<b><?php $students[$i]; ?> Average Scores</b>:
Upvotes: 1