Multiple array codeigniter

0

I'm trying to bring information from two two tables

  • the first table (separation) shows me the separation ID and time of entry
  • the second table (separacion_detalle) shows me what qualities this separation contains, as can be seen in the image, it is an example of how it should look.
  • my controller:

        public function add(){
            $data = array(
                    'separaciones' => $this->Modeltarima->getSeparaciones(),
                    'calidades' => $this->Modeltarima->getDetalle(50) 
    //Aqui no entiendo como puedo hacer esto, ¿array bidimensional? se puede ver que solo pase el numero "50" para hacer la prueba de la imagen.
                );
            $this->load->view('content/head');
            $this->load->view('content/aside');
            $this->load->view('pages/proceso/tarimas/viewgenerar',$data);
            $this->load->view('content/footer');
            $this->load->view('content/scrips/proceso/srcontrols');
        }
    

    My model:

    public function getDetalle($id){
        $this->db->select('a.*,d.id as idcal, d.nombre as cal');
        $this->db->from('separacion_detalles a');
        $this->db->distinct();
        $this->db->join('calidad d', 'a.calidad = d.id');
        $this->db->where('a.separacion',$id);
        $result = $this->db->get('separacion_detalles');
        return $result->result();
    }
    

    getDetalle(id) what it does is return the name of the qualities that belong to that separation.

    public function getSeparaciones(){
        $result = $this->db->get('separacion');
        return $result->result();
    }
    

    getSeparacion(id) we simply show the separations that are registered.

    Something a little explained in json of how I imagine it

        "separaciones": [{
            "id": "50",
            "hora": "15:04:12",
            "calidades":[ {
                "id":"10",
                "nombre":"CUARTA",
                "cajas":0
              },
               {
                "id":"30",
                "nombre":"CALIBRE 12?S",
                "cajas":0
              },
             ]
        },
    {
            "id": "51",
            "hora": "13:04:00",
            "calidades":[ {
                "id":"10",
                "nombre":"CUARTA",
                "cajas":0
              },
               {
                "id":"30",
                "nombre":"CALIBRE 12?S",
                "cajas":0
              },
             ]
        }
        ]
    

    How could I do that?

        
    asked by DoubleM 15.04.2018 в 21:45
    source

    1 answer

    1

    You can do it very simple by merging your 2 functions like this:

    public function getSeparaciones(){
        //obtienes todas las separaciones en un array
        $separaciones = $this->db->get('separacion')->result_array();
    
        //haces un foreach para recorrer el array
        foreach ($separaciones as $key => $value) {
            //luego obtienes las calidades por cada separacion
            $this->db->select('a.*,d.id as idcal, d.nombre as cal');
            $this->db->from('separacion_detalles a');
            $this->db->distinct();
            $this->db->join('calidad d', 'a.calidad = d.id');
            $this->db->where('a.separacion',$value['id']);
            $result = $this->db->get('separacion_detalles');
            //luego insertas en el array las calidades por separación
            $separaciones[$key]['calidades']=$result->result_array();
        }
        //por ultimo retornas el array de separaciones
        return $separaciones;
    }
    

    Then in your controller you call your function and you code the array in json:

    $this->load->model('datos_model');
    $data['json']=json_encode($this->datos_model->getSeparaciones());
    $this->load->view('vista', $data);
    

    and to show the data in your view you can use 2 foreach like this:

    $separaciones=json_decode($json);
    foreach ($separaciones as $value) {
        echo 'id:'.$value->id.'<br>';
        echo 'hora: '.$value->hora.'<br>Calidades: ';
        foreach ($value->calidades as $key) {
            echo '(id:'.$key->id.',';
            echo 'nombre:'.$key->nombre.',';
            echo 'cajas:'.$key->cajas.')';
        }
        echo '<br>';
    }
    

    That way you already have what you wanted to show on your json, I hope it's helpful, greetings.

        
    answered by 15.04.2018 / 23:53
    source