Good day. I have many to many relationship setup. I want to know if how can I group rows with same ids and reflect it as concatenated with each links on entries.
// 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 |
---------------------------
Here is the query (without group 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 |
---------------------------
Here is the query (with 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 |
---------------------------
Here is the actual PHP part:
<?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);
?>
The HTML/PHP part:
<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')?>
These are the tables:
----------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 |
---------------------------