Reputation: 3
I have an assignment and it requires to create a table with 2 dimensional array and the third column to report the grade according to grading system as it shows in the picture.
I made the code for table creation, but I am stuck with the conditioning for the grade column and how to apply it in the table.
Can someone please help me with this ?
<html>
<head></head>
<body>
<?php
$a=array('0'=>array('Course','Mark','Grade'),
'1'=>array('OS',90,''),
'2'=>array('MIS',85,''),
'3'=>array('WD',99,''),
'4'=>array('OOP',67,''),
'5'=>array('DS',70,''),
'6'=>array('Prog',80,'')
);
echo"<table border='2px black solid'>";
for($x=0;$x<7;$x++){
echo"<tr>";
for($y=0;$y<3;$y++){
echo "<td>".$a[$x][$y].'</td>';
}
echo"</tr>";
}
echo"</table>";
?>
</body>
</html>
Upvotes: 0
Views: 10010
Reputation: 1049
Simple and easy ;)
<html>
<head></head>
<body>
<?php
$data = array(
array('Course', 'Mark', 'Grade'),
array('OS', 90, ''),
array('MIS', 85, ''),
array('WD', 99, ''),
array('OOP', 67, ''),
array('DS', 70, ''),
array('Prog', 80, ''),
array('Prog', 50, ''),
array('Prog', 40, ''),
array('Prog', 30, ''),
array('Prog', 20, ''),
array('Prog', 10, ''),
array('Prog', 0, '')
);
$grade_chars = array('E', 6 => 'D', 'C', 'B', 'A');
echo "<table border='2px black solid'>";
foreach($data as $row) {
echo "<tr>";
if($row[2] == '') {
$val = (int)($row[1] / 10);
$row[2] = $grade_chars[$val > 5 ? $val : 0];
}
foreach($row as $col) {
echo "<td>" . $col . '</td>';
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Upvotes: 0
Reputation: 1599
Stackoverflow is not for answering assignment questions, but it seems like you have put your effort. You have to display grade in the third column ie, when $y =2 (0,1,2). So you can do it like this.
<html>
<head></head>
<body>
<?php
$a=array('0'=>array('Course','Mark','Grade'),
'1'=>array('OS',90,''),
'2'=>array('MIS',85,''),
'3'=>array('WD',99,''),
'4'=>array('OOP',67,''),
'5'=>array('DS',70,''),
'6'=>array('Prog',80,'')
);
echo"<table border='2px black solid'>";
for($x=0;$x<7;$x++){
echo"<tr>";
for($y=0;$y<3;$y++){
if($y == 2)
{
//Your grade logic
$grade = logic($a[$x][1]); //You can define some function that takes marks and return grade
echo "<td>".$grade.'</td>';
}
else
{
echo "<td>".$a[$x][$y].'</td>';
}
}
echo"</tr>";
}
echo"</table>";
?>
</body>
</html>
Edit
<html>
<head></head>
<body>
<?php
$a=array('0'=>array('Course','Mark','Grade'),
'1'=>array('OS',90,''),
'2'=>array('MIS',85,''),
'3'=>array('WD',99,''),
'4'=>array('OOP',67,''),
'5'=>array('DS',70,''),
'6'=>array('Prog',80,'')
);
echo"<table border='2px black solid'>";
for($x=0;$x<7;$x++){
echo"<tr>";
for($y=0;$y<3;$y++){
if($y == 2)
{
//Your grade logic
$marks = $a[$x][1];
if($marks > 90)
{
$grade = 'A';
}
else if($marks > 80)
{
$grade = 'B';
}
else if($marks > 70)
{
$grade = 'C';
}
else if($marks > 60)
{
$grade = 'D';
}
else
{
$grade = 'E';
}
echo "<td>".$grade.'</td>';
}
else
{
echo "<td>".$a[$x][$y].'</td>';
}
}
echo"</tr>";
}
echo"</table>";
?>
</body>
</html>
Upvotes: 2
Reputation: 11749
First define your grade logic function...
// Define your Grade Logic function
function getGrade($grade){
if($grade>20 && $grade<40){return 'D';}
if($grade>=40 && $grade<=60){return 'C';}
if($grade>=61 && $grade<=80){return 'B';}
if($grade>=81 && $grade<=100){return 'A';}
}
Then apply it in your loop...
// Start table
echo "<table border='2px black solid'>";
//Loop through your array
foreach($a as $k=>$v){
// If key is 0, means its the column row, echo headers
if($k==0){
echo "<tr><td>".$a[$k][0]."</td><td>".$a[$k][1]."</td><td>".$a[$k][2]."</td></tr>";
} else {
// Else echo the table TD's
// Start table row
echo "<tr>";
// loop through the array of grades for each row
foreach($v as $key=>$val){
echo "<td>";
// If key is 1, that means we get the grade from our function
if($key==1){
$grade = getGrade($val);
}
if($key==2){
// If key is 2 that means we need to echo the $grade
echo $grade;
} else {
// else we just echo the array value
echo $val;
}
echo "</td>";
}
// Finish table row
echo "</tr>";
}
}
// End table
echo "</table>";
Upvotes: 0