多对多关系设置中的php选择

qq24tv8q  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(295)

很好的一天。我有很多对很多的关系。我想知道如何将具有相同ID的行分组,并将其反映为与条目上的每个链接串联在一起。

// from this                          // to this but with each link
----------Classes----------           ----------Classes----------
| Student Id | Lecture ID |           | Student Id | Lecture ID |
|     1      |      1     |           |     1      |  1; 2; 3   |
|     1      |      2     |           |     1      |    4; 2    |
|     1      |      3     |           ---------------------------
|     2      |      4     |           
|     2      |      5     |
---------------------------

以下是查询(不带组concat):

$classes = "SELECT * FROM students, lectures, classes
WHERE students.student_id=classes.student_id
AND lectures.lecture_id=classes.lecture_id"

$class_result = mysqli_query($con, $classes);

while($class = mysqli_fetch_array($class_result){
  echo '<table><tr>';
  echo '<td><a href="student.php?id='.$class['student_id'].'">'.$class['student'].'</a></td>';
  echo '<td><a href="lecture.php?id='.$class['lecture_id'].'">'.$class['lecture'].'</a></td>';
  echo '</tr></table>';
}

----------Classes----------
| Student Id | Lecture ID |
|     1      |      1     |
|     1      |      2     |
|     1      |      3     |
|     2      |      4     |           
|     2      |      5     |
---------------------------

以下是查询(使用组concat):

$classes = "SELECT students.student_id, student, lectures.lecture_id, GROUP_CONCAT(lecture SEPARATOR '; ')
FROM students, lectures, classes
WHERE students.student_id=classes.student_id
AND lectures.lecture_id=classes.lecture_id
GROUP BY student"

$class_result = mysqli_query($con, $classes);

while($class = mysqli_fetch_array($class_result){
  echo '<table><tr>';
  echo '<td><a href="student.php?id='.$class['student_id'].'">'.$class['student'].'</a></td>';
  echo '<td><a href="lecture.php?id='.$class['lecture_id'].'">'.$class["GROUP_CONCAT(lecture SEPARATOR '; ')"].'</a></td>';
  echo '</tr></table>';
}

----------Classes----------
| Student Id | Lecture ID |  // this works but it cannot reflect each link of id of lecture
|     1      |  1; 2; 3   |  // it only reflects link of one id for lecture
|     1      |    4; 2    |
---------------------------

下面是实际的php部分:

<?php include ('connect.php'); ?>

<?php

$classes = "SELECT * FROM students, lectures, classes WHERE students.student_id=classes.student_id AND lectures.lecture_id=classes.lecture_id";

$class_result = mysqli_query($con, $classes);

?>

html/php部分:

<html>
<title>Classes</title>
<body>
<table border="1" cellspacing="0" cellpadding="10" width="500">
<tr>
    <td colspan="2" align="right">
    <a href="add_class.php">Add Class</a>
    </td>
</tr>
<tr>
    <th>Student</th>
    <th>Lecture</th>
</tr>
<?php while($class = mysqli_fetch_array($class_result)) { ?>
<tr>
    <td align="center"><a href="student.php?student_id=<?=$class['student_id']?>"><?=$class['student']?></a></td>
    <td align="center"><a href="lecture.php?lecture_id=<?=$class['lecture_id']?>"><?=$class['lecture']?></a></td>
</tr>
<?php } ?>
</table>
</body>
</html>

<?php include('close.php')?>

以下是表格:

----------Classes----------   // wanted to    ----------Classes----------
|  Student   |  Lecture   |       be like     |  Student   |  Lecture   | 
|   John     |    Math    |        this       |            |    Math;   |
|   John     | Literature |        --->       |   John     | Literature;|
|   Paul     |    Math    |      clickable    |            |  Physics;  |
|   Paul     |   Speech   |        each       |            |   Speech   |
|   Paul     | Literature |       lecture     ---------------------------
|   John     |   Physics  |                   |            |    Math;   |
|   Paul     |   Physics  |                   |   Paul     |   Speech;  |
|   John     |   Speech   |                   |            | Literature;|
---------------------------                   |            |  Physics   |
                                              ---------------------------
ewm0tg9j

ewm0tg9j1#

<?php
$conn=mysqli_connect("localhost","root","","database");

$student_query="SELECT student_id FROM classes";
$student_result=mysqli_query($conn,$student_query);
while($row_student=$student_result->fetch_assoc())
{
   $student_id=$row_student['student_id'];

   $lecturer_query="SELECT lecturer_id FROM classes where student_id='$$student_id'";

   $lecturer_result=mysqli_query($conn,$lecturer_query);
   while($row_lecturer=$lecturer_result->fetch_assoc())
  { 
       $lecturer_id[]=$row_lecturer['lecturer_id'];
  }

  $result[]=array('student_id'=>$student_id,'lecturer_id'=>$lecturer_id);
}

  echo json_encode($result);
9udxz4iz

9udxz4iz2#

我可能会这样做:

$classes = "SELECT * FROM students, lectures, classes
WHERE students.student_id=classes.student_id
AND lectures.lecture_id=classes.lecture_id  ORDER BY students.student_id";

$class_result = mysqli_query($con, $classes);

$student_ids = [];

while($class = mysqli_fetch_array($class_result)){
  //fixed missing ) - while($class = mysqli_fetch_array($class_result){

  if(!isset($student_ids[$class['student_id']])){
       if(count($student_ids) > 1) echo '</tr></table>'; //close previous list
       $student_ids[$class['student_id']] = true;
       echo '<table><tr>';
       echo '<td><a href="student.php?id='.$class['student_id'].'">'.$class['student'].'</a></td>';
  }
  echo '<td><a href="lecture.php?id='.$class['lecture_id'].'">'.$class['lecture'].'</a></td>';
}

if(count($student_ids) > 1) echo '</tr></table>'; //close last list

注意 Order By 以及 $student_ids 数组。这样你就可以
学生链接
第1讲链接
第2讲链接
第3讲链接
学生b链接
第1讲链接
等。。。
我建议使用描述列表:

<dl>
   <dt>Student A</td>
   <dd>Lecture 1</dd>
   <dd>Lecture 2</dd>
</dl>

但是你必须在上面做一些css。对我来说,这似乎是一个更好的结构,因为它可以让你消除一些标签,并给学生链接一个不同的标签。但这取决于你。
在任何情况下,html结构和“样式”都是一个次要的细节,一旦您以您想要的方式获得了数据。更不用说,鉴于问题中提供的信息,我们无法知道您希望它看起来如何。
也就是说,似乎讲座链接是独立于学生的。在我看来,它应该包括一些从学生或链接是不相关的东西。我的意思是两者之间的区别。
学生a,第1课

学生乙,第一讲
两个讲座链接是相同的。

ldfqzlk8

ldfqzlk83#

终于在所有的寻找和获取的想法。我终于成功了。代码就是这样的。

<?php

$classes = "SELECT * FROM students, lectures, classes WHERE students.student_id=classes.student_id AND lectures.lecture_id=classes.lecture_id GROUP BY students.student_id";

$class_result = mysqli_query($con, $classes);

while($class = mysqli_fetch_array($class_result)) {
    $student_id = $class['student_id'];
    $lectures = mysqli_query($con,"SELECT * FROM students, lectures, classes WHERE students.student_id=classes.student_id AND lectures.lecture_id=classes.lecture_id AND students.student_id='$student_id'");
    $counter = 0;
?>
    <tr>
        <td align="center"><a href="student.php?student_id=<?=$class['student_id']?>"><?=$class['student']?></a></td>
        <td align="center">
        <?php while($lecture = mysqli_fetch_array($lectures)) { ?>
            <a href="lecture.php?lecture_id=<?=$lecture['lecture_id']?>"><?=$lecture['lecture'];?></a>
        <?php } ?>
        </td>
    </tr>
<?php } ?>

现在该表如下所示:

-----------------------Classes----------------------------
|    Student    |              Lectures                  |
----------------------------------------------------------
|     John      |    Math Literature Physics Speech      |
----------------------------------------------------------
|     Paul      |    Math Speech Literature Physics      |
----------------------------------------------------------

现在我将在讲座中讨论分隔符/分隔符(e、 g.数学;文学;物理学;演讲)。谢谢你的帮助。

相关问题