选择多对多关系设置

时间:2018-01-17 06:02:01

标签: php mysql select mysqli many-to-many

美好的一天。我有很多关系设置。我想知道如何对具有相同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     |
---------------------------

这是查询(没有组连接):

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

这是查询(使用group 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   |
                                              ---------------------------

3 个答案:

答案 0 :(得分:0)

我可能会这样做:

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

学生B,第1讲

两个讲座链接都是一样的。

答案 1 :(得分:0)

<?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);

答案 2 :(得分:0)

最后经过所有的搜索和提取想法。我终于成功完成了这项工作。这就是代码的用法。

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

现在我将处理讲座的分隔符/分隔符。 (例如数学;文学;物理;演讲)。谢谢你的帮助。