Pass several variables from various controller types to codeigniter view

0

Good afternoon,

I generate the following query to the database to bring a list with the id and the name of a product

function getActivos(){
     // armamos la consulta 
     $query = $this->db
                ->select("*")
                ->from("tipo_activo")
                ->order_by("id_tipo_activo")
                ->get();
     // si hay resultados 
     //echo $this->db->last_query();exit; 
     if ($query->num_rows() > 0) { 
     // almacenamos en una matriz bidimensional 
        foreach($query->result() as $row) $arrDatos[htmlspecialchars($row->id_tipo_activo, ENT_QUOTES)] = htmlspecialchars($row->nombre_tipo_activo, ENT_QUOTES); 
        $query->free_result(); 
        return $arrDatos; 
        } 
    }

in the controller I have the following function to load the result of the query:

public function modificar($id=null)
    {
        //obliga a que se le pase un parametro por la url, que es el id del activo a modificar
        if(!$id){show_404();}
        $datos=$this->activo_model->getTodosPorId($id);
        if(sizeof($datos)==0){show_404();}
        //print_r($datos);exit;
        // obtenemos el array de activos y lo preparamos para enviar 
        $getdatos['arrayActivos'] = $this->activo_model->getActivos(); 
        //print_r($getdatos);exit;

        $this->layout->view("modificar",compact('datos','id', 'getdatos'));
    }

and in the view I post the list as follows:

<p>
                    <label for="tipo_activo" >Tipo Activo:</label>
                    <select name="tipo_activo" class="form-control" value="<?php echo set_value_input(array(),'tipo_activo','tipo_activo')?>" autofocus="true" >
                    <option selected value="0"></option>
                    <?php foreach ($arrayActivos as $i => $nombre_tipo_activo) {
                        echo '<option value"',$i,'">', $nombre_tipo_activo,'</option>';
                    }
                    ?>
                    </select>
                    </p>

The problem I have is that when I call the view from the controller as it relates previously:

$this->layout->view("modificar",compact('datos','id', 'getdatos')); 

I do not load the list of the query that I made, in fact, if I do it in the following way:

$this->layout->view("modificar",$getdatos); 

if I load it, but I can not leave it that way because I also need to load $ data and $ id that are being loaded with a compact, if I charge it like this:

$this->layout->view("modificar",$getdatos, $id, $datos);

does not work for me either, what can I do to load the three variables and load the list?

thank you very much

    
asked by Daniel Suarez 12.11.2017 в 07:46
source

2 answers

0

Check the array that you send to the view with var_dump( compact('datos','id', 'getdatos') ) , also try replacing compact() with array_merge() to join several array. Greetings.

    
answered by 15.11.2017 в 13:45
0

If you want to pass the 3 parameters without any problem you can do it in the following way, in a variable with the same name but with different arrays internal%.

$data['id'] = $id;
$data['datos'] = $this->activo_model->getTodosPorId($id);
$data['getdatos'] = $this->activo_model->getActivos();

and to see these data, you do it with:

$this->layout->view("modificar",$data); 

To access that data in the view you only send the variable $id , $datos and $getdatos , each one is independent.

    
answered by 20.07.2018 в 21:48