dqfsbvd43312 2015-12-15 05:58 采纳率: 0%
浏览 85
已采纳

我不能从已经使用GROUP BY的表中循环记录

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!

  • 写回答

1条回答 默认 最新

  • dsdioa9545 2015-12-15 06:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路