user3067476 user3067476 - 1 month ago 5
MySQL Question

How to view many to many tables on side by side column

I have table which is called as 'kelas'.

|id_kelas|nama_kelas|


I have table Sesi

|id_sesi|nama_sesi|


and I have another table which is sesikelas

|id_kelas|id_sesi|


many to many relation is there between kelas to sesi, what I want with PHP programming is that to show/view value of sesi on sesikelas side by side another on 2 colum.

example

|id_kelas|nama_kelas|session1|session2|
|01 |nama_kelas|id_sesi |id_sesi |


sesion1 or session2 come from the value on the table sesikelas.
the problem is, I want to combine the the id_sesi which is in equal to certain id_kelas. not one by one in a row, but if there is two record of id_kelas on table sesikelas will be display side by side.

Can anyone help me with php, how to view them exactly like in picture?

my PHP, but doesn't work properly.
I use mysql database, here is my php code.

$cls=mysql_query("select k.id_kelas,nama_kelas, sk.id_sesi,sesi from kelas k left join sesikelas sk on k.id_kelas=sk.id_kelas left join sesi on sk.id_sesi=sesi.id_sesi order by k.id_kelas");
while($fr = mysql_fetch_array($cls))
{
if ($temp2!=$fr['id_kelas'])
{
echo "<tr><td class='idclass'>".$fr['id_kelas']."</td>";
echo "<td class='namecell'>".$fr['nama_kelas']."</td>";
if(isset($fr['sesi']))
{
$x=mb_substr($fr['sesi'],0,1);
echo "<td class='ses1cell'>".$hari[$x]." (".mb_substr($fr['sesi'],1,2).".".mb_substr($fr['sesi'],3,2)."-".mb_substr($fr['sesi'],5,2).".".mb_substr($fr['sesi'],7,2).")</td>";
} else {
echo "<td class='ses1cell'>Not set</td>";
}
$temp='skip';
} else {
$temp='session2';
}

if (($temp=='session2'))
{
if(isset($fr['sesi']))
{
$x=mb_substr($fr['sesi'],0,1);
echo "<td class='ses2cell'>".$hari[$x]." (".mb_substr($fr['sesi'],1,2).".".mb_substr($fr['sesi'],3,2)."-".mb_substr($fr['sesi'],5,2).".".mb_substr($fr['sesi'],7,2).")</td>";
}else{echo "<td class='ses2cell'>Not set</td>";}
echo "<td align='right'><a class='edit' href='#' onClick='Edit($(this))'>Edit</a> |
<a class='delete' href='#' onClick='DeleteData($(this))'>Delete</a></td>";
echo "</tr>";
$temp='skip';
}

if (($temp=='skip') and ($temp2==$fr['id_kelas']))
{ if ($temp2==$fr['id_kelas']){
echo "<td class='ses2cell'>Not ff set</td>";}
echo "<td align='right'><a class='edit' href='#' onClick='Edit($(this))'>Edit</a> |
<a class='delete' href='#' onClick='DeleteData($(this))'>Delete</a></td>";
echo "</tr>";

}
$temp2=$fr['id_kelas'];
}

Answer

Your question isn't very clear, but I suspect this is what you want:

SELECT k.id_kelas, k.nama_kelas, s.session1, s.session2
FROM kelas k
JOIN sesikelas sk ON k.id_kelas = sk.id_kelas
JOIN sesi s ON s.id_sesi = sk.id_sesi

This is the normal way to join two tables using an intermediate relation table.

Comments