如何使用所有模块返回JSON?

时间:2016-08-16 23:03:44

标签: php json codeigniter

我想用这样的所有模块获取我的JSON:

[
 {
  "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"
       }
      ]
     }
    ]

但我的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
    }
 }

控制器刚刚返回我的最后一个模块,但我不知道为什么。如何修复它并更改我的控制器方法以返回所有模块?

控制器

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();
}

查看

<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>

1 个答案:

答案 0 :(得分:0)

您将在第一个&#34; foreach&#34;中覆盖您的结果。声明:

$menu['seccions'] = $modules;

在每个周期中,如果SQL查询成功,则只需用当前值覆盖所有以前的数据。

它后来对应于输出:

{
 "seccions":
 {
   "0": { ... }
 }
}

您的控制器代码应如下所示:

public function getModules($module_id){
    if ($data = $this->session->userdata('log')){
        $seccions = $this->module->get_rows();
        foreach ($seccions as $index => $seccion) {
            // Consider using fetchAll() or similar method
            $seccions[$index]['data'] = $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");
        }
        $this->json($seccions);
        $this->load->view('modules_view', $seccions);
    }
}

只需考虑在$ this-&gt; module-&gt; query()之后使用fetchAll()或类似方法一次获取所有数据。