【问题标题】:Selecting in many to many relationship setup在多对多关系设置中选择
【发布时间】:2018-06-25 22:40:50
【问题描述】:

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

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

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

【问题讨论】:

  • 您可以使用explode(';', $class['grouped']),但您应该知道group_concat 有大小限制,我也会去掉分隔符中的那个空间,并为该字段设置别名。 GROUP_CONCAT(lecture SEPARATOR '; ') AS grouped 或者您可以按您要分组的字段对其进行排序...
  • 如果您使用 PDO,我会使用 FETCH_GROUP。对于 mysqli 我不确定。

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


【解决方案1】:
<?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);

【讨论】:

  • 我想试试这些@Mazhar Hussain。要是能详细解释就好了。谢谢。
  • 你知道php编码吗
  • 我已经编辑了我的答案,这是完美的代码。输入并运行正常
【解决方案2】:

我可能会这样做:

$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 数组。这样你就会得到 ​​p>

  • 学生A链接
    • 讲座1链接
    • 第二讲链接
    • 第三讲链接
  • 学生B链接
    • 讲座1链接

等等……

我建议使用描述列表:

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

但您必须对其进行一些 CSS 处理。对我来说,这似乎是一个更好的结构,因为它可以让您消除一些标签并为学生链接提供不同的标签。但这取决于你。

无论如何,一旦您以您想要的方式获得数据,HTML 结构和“样式”就是一个次要细节。更不用说鉴于问题中提供的信息,您无法知道您希望它的外观。

也就是说,讲座链接似乎与学生无关。在我看来,它应该包括学生的一些东西,或者链接是不相关的。我的意思是两者有什么区别。

学生 A,第 1 课

学生 B,第 1 讲

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

【讨论】:

  • 这适用于@ArtisticPhoenix,但它没有显示为一行。在名称下方出现了许多行以及相应的讲座。
  • 我修复了这个问题,只在添加学生时添加&lt;table&gt;&lt;tr&gt;,如果添加了学生,则在循环后关闭它。那应该只保留一个循环。
  • 原始问题中缺少),我只是复制并粘贴了它。这里while($class = mysqli_fetch_array($class_result){
【解决方案3】:

终于在所有的搜索和获取想法之后。我终于设法完成了这项工作。这就是代码的运行方式。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-26
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2014-07-22
    • 1970-01-01
    相关资源
    最近更新 更多