gpt4 book ai didi

php - 如何在 PHP 中将单元格按列分组

转载 作者:行者123 更新时间:2023-11-28 05:55:46 24 4
gpt4 key购买 nike

我已经用 PHP 编写了代码(在下面列出),但遇到了一个小问题......

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

当我运行代码时,我看到以下输出:

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

但我不希望那样。我希望术语 cells 变成一个,像这样:

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

或以任何其他不让术语重复的方式,我的意思是:将具有相同术语的类(class)组合在一起。

最佳答案

试试这段代码,

$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>";

关于php - 如何在 PHP 中将单元格按列分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37085918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com