如何使用codeigniter在两个表中获取数据菜单和子菜单

时间:2017-10-14 00:42:33

标签: php mysql codeigniter

我有问题。如何根据相应的菜单ID

连接2个表格并显示子菜单

我的数据库: enter image description here

我的模特是

function get_all_bantuan(){
    $this->db->select('*');
    $this->db->from('kategori');
    $r = $this->db->get();
    return $r->result();
}

function get_isi_kategori_bantuan(){
    $this->db->select('*');
    $this->db->from('kategori a');
    $this->db->join('menu_kategori b', 'a.id_kategori=b.id_kat');
    $this->db->where('b.status','on');
    $r = $this->db->get();
    return $r->result_array();
}

我的控制员:

$data['info'] = $this->Halaman_bantuan_m->get_all_bantuan();
$data['sub_info'] = $this->Halaman_bantuan_m->get_isi_kategori_bantuan();

$this->load->view('headerHelp',$data);

我的观点是:

<div class="row">
    <?php 
        foreach($info as $row){ 
    ?>
        <div class="col-md-3 col-xs-12 dsg">
            <div class="hlp co_num1">
                <h3 class="djnl"><?php echo $row->kategori?></h3>
                <ul class="list-unstyled">
                <?php 
                    foreach($sub_info as $row){ 
                ?>
                    <li class="ct-sub"><a href="<?php echo base_url('bantuan/');?><?php echo $row['slug'];?>"><?php echo $row['menu'];?> <i class="glyphicon glyphicon-play plb pull-right"></i></a></li>
                <?php }?>
                </ul>
            </div>
        </div>
    <?php }?>
</div>

1 个答案:

答案 0 :(得分:0)

不需要where条件,因为数据库表中没有名为status的字段。

return $this->db->join('menu_kategori', 'id_kategori=id_kat','left')->get('kategori')->result_array();