我怎样才能让我的json回归正常?

时间:2016-08-16 18:47:25

标签: php codeigniter

这个函数用于检查模块用户有权限,但是当我使用这个函数返回我时,像json不显示所有模块。它只是告诉我最后一个。

控制器

public function getModules($module_id){
     if ($this->session->userdata('log')){
         $data = $this->session->userdata('log');
         $menu = array();
         $seccions = $this->module->get_rows();
         foreach ($seccions as $index => $seccion) {
             $modules = $this->module->query("SELECT CONCAT('".$seccion['id']."',storelte_modulo.id) AS id,CONCAT('".base_url('assets/img/sidebar')."','/',storelte_modulo.icon) as icon, storelte_modulo.modulo AS value,storelte_modulo.seccion_id,CONCAT('".base_url()."',storelte_modulo.url) AS  url FROM storelte_modulo INNER JOIN storelte_modulo_perfil ON  storelte_modulo_perfil.modulo_id = storelte_modulo.id WHERE seccion_id = $seccion[id] AND storelte_modulo_perfil.perfiles_id = $data[id] AND storelte_modulo_perfil.STATUS = 1");
             //assuming you get 1 row only from the above query
             if (!empty($modules)) {
                    $modules['index'] = $index;
                    $menu['seccions'] = $modules;
             }
         }
         foreach ($seccions as $item) {
             array_push($menu,$item);
         }
         $this->json($menu);
         $this->load->view('modules_view',$menu);
     }
 }

模型

public function get_rows(){
        $this->db->select('id,seccion');
        $this->db->from('storelte_seccion');
        return $this->db->get()->result_array();
    }

    public function query($query){
        return $this->db->query($query)->result_array();
    }

查看HTML

<div class="row">
<h3 class="text-center">Welcome to storeLTE, click a module below to get started!</h3>
<div class="home_module_list">
    <div class="module_item">
        <?php foreach ($seccions as $session) { ?>
          <div class="module_item" title="<?= $session['value'];?>">
            <a href="<?= $session['url']; ?>"><img src="<?= $session['icon']; ?>"/></a>
            <a href="<?= $session['url']; ?>"><?= $session['value']?></a>
        </div>
        <?php } ?>
    </div>
</div>
</div>

我的控制器给我发了这个json

{"seccions":{"0":{"id":"22","icon":"http:\/\/localhost\/storeLTE\/assets\/img\/sidebar\/suppliers.png","value":"Compras","seccion_id":"2","url":"http:\/\/localhost\/storeLTE\/compras\/compras"},"index":1}}

我想像这样回复它:

[{"id":"1","seccion":"Inventario","data":[{"id":"11","icon":"http:\/\/localhost\/storeLTE\/assets\/img\/sidebar\/items.png","value":"Productos","seccion_id":"1","url":"http:\/\/localhost\/storeLTE\/inventario\/product"},{"id":"14","icon":"http:\/\/localhost\/storeLTE\/assets\/img\/sidebar\/employees.png","value":"Provedores","seccion_id":"1","url":"http:\/\/localhost\/storeLTE\/provedor\/provedor"}]},{"id":"2","seccion":"Compras","data":[{"id":"22","icon":"http:\/\/localhost\/storeLTE\/assets\/img\/sidebar\/suppliers.png","value":"Compras","seccion_id":"2","url":"http:\/\/localhost\/storeLTE\/compras\/compras"}]}]

0 个答案:

没有答案