Php 如何在HTML语言中修改表中的行
我有这个密码Php 如何在HTML语言中修改表中的行,php,html,css,sql,Php,Html,Css,Sql,我有这个密码 <html> <head> <title> trans </title> <body> <style> table, th, td { border: 1px solid black; width: 900px; margin: auto; } h3{ text-align: center; margin-top: 20px; } </style> <?php $con = mysq
<html>
<head>
<title> trans </title>
<body>
<style>
table, th, td {
border: 1px solid black;
width: 900px;
margin: auto;
}
h3{
text-align: center;
margin-top: 20px;
}
</style>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM `degree_plan` LEFT JOIN courses ON
degree_plan.course_number=courses.course_number
where major='COE'";
$result = mysqli_query($con , $q) ;
if($result){
echo "<br />";
echo "<table>";
echo "<tr>";
echo "<th>courses</th>";
echo "<th>term_no</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "<td>" . $row["term_no"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
我如何才能使代码不分离具有相同学期的课程。
我想要这样的输出
--------------------------
course term_no
--------------------------
CHEM 101 1
ENGL 101
-------------------------
MATH 101 2
PE 101
您可以创建一个字符串数组,其中每个单元格包含与单元格id匹配的同一术语中的所有课程 在当前while循环的末尾尝试以下操作:
$str = array();
while($row = mysqli_fetch_array($result))
{
$str[$row['term_no']] += ",".$row['code'];
}
foreach ($str as $key => $value)
{
echo "<tr>";
echo "<td>" . $value. "</td>";
echo "<td>" . $key. "</td>";
echo "</tr>";
}
$str=array();
while($row=mysqli\u fetch\u数组($result))
{
$str[$row['term_no']+=“,”$row['code'];
}
foreach($str作为$key=>$value)
{
回声“;
回显“$value.”;
回显“$key.”;
回声“;
}
编辑:我更改了代码,但现在尝试一下。以下是我的做法: 我会将您的sql命令更改为具有相同term_no值的group和concat结果
SELECT group_concat(c.course), dp.term_no FROM `degree_plan` as dp LEFT JOIN courses as c ON
degree_plan.course_number=courses.course_number
where major='COE'
GROUP BY dp.term_no
完成后,我将恢复由,
连接和分隔的数据。有了这些,我可以做preg_replace(“/,/”,“
”,$row[“code”])
来替换每个,
,或者我可以在迭代中分解所需的值(explode(“,”,$value)
),创建一个数组,然后做我想做的事情
请注意,因为我不知道数据库的确切结构,所以我假设表course包含课程的名称。进行必要的调整。您的意思是要将所有具有相同术语的课程分组在一起?可能首先要做一个数组;)是的,我想把所有的课程用相同的术语组合在一起。我想你应该先在sql查询中输入一个ORDER by。
SELECT group_concat(c.course), dp.term_no FROM `degree_plan` as dp LEFT JOIN courses as c ON
degree_plan.course_number=courses.course_number
where major='COE'
GROUP BY dp.term_no