Reputation: 815
I want to loop record of field from a table. I use LEFT JOIN in my query and GROUP BY to prevent double loop in result. The query is successful but the result like this :
===========================================
|No|Nama Sekolah|Alamat|Telepon|Kompetensi|
===========================================
|1 |SMP 1 |JKT |021231 | 1.TIK1 |
|2 |SMP 2 |BDG |021232 | 1.RPL1 |
===========================================
If im not use GROUP BY the result will be like this :
===========================================
|No|Nama Sekolah|Alamat|Telepon|Kompetensi|
===========================================
|1 |SMP 1 |JKT |021231 | 1.TIK1 |
|2 |SMP 1 |JKT |021231 | 1.TIK2 |
|3 |SMP 2 |BDG |021232 | 1.RPL1 |
|4 |SMP 2 |BDG |021232 | 1.RPL2 |
===========================================
I want the result like this :
===========================================
|No|Nama Sekolah|Alamat|Telepon|Kompetensi|
===========================================
|1 |SMP 1 |JKT |021231 | 1.TIK1 |
| | | | | 2.TIK2 |
|2 |SMP 2 |BDG |021232 | 1.RPL1 |
| | | | | 2.RPL2 |
===========================================
Im using codeigniter framework.
Here is my view :
<table class='table table-bordered'>
<thead>
<tr>
<td>No</td>
<td>Lokasi</td>
<td>Alamat</td>
<td>Telepon</td>
<td>Kompetensi</td>
</tr>
</thead>
<?php
$i = 0;
foreach ($row as $row) {
$i++;
echo"<tr>
<td>".$i."</td>
<td>".$row->nama_sekolah."</td>
<td>".$row->alamat."</td>
<td>".$row->no_telp."</td>
<td><ol>
<li>".$row->nama_jurusan."</li>
</ol>
</td>
</tr>";
}
?>
</table>
Here is model :
function getlokasi($jenjang){
$sql = "SELECT s.nama_sekolah, s.alamat, s.no_telp, j.nama_jurusan FROM sekolah s LEFT JOIN jurusan j ON j.id_sekolah = s.id_sekolah WHERE s.id_jenjang = '".$jenjang."' GROUP BY s.id_sekolah";
$q = $this->db->query($sql);
return $q->result();
}
Please help me. Thanks!
Upvotes: 2
Views: 113
Reputation: 573
So basically you want to show ol
for TKL1,TKL2. If so
first use group_concat
for j.nama_jurusan
like
GROUP_CONCAT(DISTINCT j.nama_jurusan SEPARATOR ',')
in your query
and then in view explode the value from ,
and then use foreach loop to print it in li
just a guess, hope it will help.
Upvotes: 5