我正在做学校项目。我为教师和科目制作了一个联结表。 Teacher 表包含 t_id 和 t_name,Subjects 表包含 sub_id 和 sub_name。而在连接名称中,我创建了 t_id 和 sub_id 的外键。现在,当我选择多个主题然后将其显示在用户界面中时。如果我选择 3 个科目,它会重复 t_id 3 次。我只想显示一次 t_id 和 3 个 sub_id,并在表中用逗号分隔它们。 Click here to see the screenshot of the current table 。
这是我当前编写的代码:
<?php
$que = "select teacher.t_name,teacher.qualification,teacher.gender,subject.sub_name from teacher join teach_sub_junction on teacher.t_id=teach_sub_junction.teacher_id join subject on subject.sub_id=teach_sub_junction.subject_id";
$i = 1;
if($row = mysql_query($que)){
while($result = mysql_fetch_assoc($row)){
$tname = $result['t_name'];
$qualification = $result['qualification'];
$gender = $result['gender'];
$sub_name = $result['sub_name'];
?>
<tr>
<td><?php echo $i; $i++; ?></td>
<td><?php echo $tname; ?></td>
<td><?php echo $gender; ?></td>
<td><?php echo $qualification; ?></td>
<td><?php echo $sub_name; ?></td>
</tr>
<?php }
}
?>
最佳答案
您可以使用 GROUP_CONCAT(subject.sub_id) 和 GROUP BY Teacher.t_id 来显示一次 t_id,并在表格中用逗号分隔 3 个 sub_id。
关于php - 在 PHP MySQL 中使用 Explode 显示连接表中的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43704470/