ABHAY ABHAY - 7 months ago 8
HTML Question

How to group cells together in columns, in PHP

I have written code in PHP (listed below,) but face a small problem...

<html>
<head>
<title> trans </title>
<body>
<style>
table, th, td {
border: 1px solid black;
width: 700px;
margin: auto;
}
</style>
<?php

$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}

mysqli_select_db($con,"uoh");


$q1 = "SELECT * FROM student_record INNER JOIN degree_plan ON
student_record.course_number = degree_plan.course_number
INNER JOIN courses ON student_record.course_number =
courses.course_number where student_record.id = 201102887 AND degree_plan.major='COE'";

$result = mysqli_query($con , $q1 ) ;
if($result){
echo "<table>";
echo "<tr>";
echo "<th>courses</th>";
echo "<th>terms</th>";
echo "<th>grades</th>";
echo "<th>CRD</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{

echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "<td>" . $row["term_no"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
echo "</table>";
}

?>

</body>
</html>


When I run the code I see the following output:

---------------------------------------
| courses | terms | grades |
---------------------------------------
| CHEM 101 | 1 | A |
---------------------------------------
| ENGL 101 | 1 | C+ |
---------------------------------------
| PE 101 | 1 | B |
---------------------------------------
| PHYS 101 | 1 | F |
---------------------------------------
| ENGL 102 | 2 | B+ |
---------------------------------------
| PE 101 | 2 | B |
---------------------------------------
| PHYS 102 | 2 | D+ |
---------------------------------------
| MATH 201 | 3 | A |
---------------------------------------
| COE 200 | 3 | B |
---------------------------------------


But I do not want it like that. I want the term cells to become one, like this:

---------------------------------------
| courses | term | grade |
---------------------------------------
| CHEM 101 1 A |
| ENGL 101 C+ |
| PE 101 B |
| PHYS 101 F |
---------------------------------------
| ENGL 102 2 B+ |
| PE 101 B |
| PHYS 102 D+ |
---------------------------------------
| MATH 201 3 A |
| COE 200 B |
---------------------------------------


or in any other way that does not let term repeat, by which I mean: group the courses that have the same term together.

Answer

try this code,

$data = array();
while($row = mysqli_fetch_array($result))
{
    $data[$row["term_no"]][] =  array(
        'code' => $row["code"],
        'grade' => $row["grade"]
    );

}


echo '<table width="200" border="1">';
   echo "<tr>";
   echo "<th>courses</th>";
   echo "<th>terms</th>";
   echo "<th>grades</th>";
   echo "</tr>";

foreach($data as $term=>$otherrow) {
    $count = 0;
    foreach ($otherrow as $data) {
        if($count == 0) {
            echo "<tr>";
            echo "<td>" . $data["code"]. "</td>";
            echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
            echo "<td>" . $data["grade"]. "</td>";
            echo "</tr>";
        }else {
            echo "<tr>";
            echo "<td>" . $data["code"]. "</td>";
            echo "<td>" . $data["grade"]. "</td>";
            echo "</tr>";
        }
        $count++;
    }
}
echo "</table>";