Load two dependent combos at the same time from the database

0

Good I am trying to load two combos at the same time but like that you can not if I only show a combo there is no problem but the problem is when I load the two combos; Here is the code

The users.php driver

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Usuarios extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Usuarios_model');
    }



    public function index()
    {
  // $this->load->view('frontend/usuarios');
// obtenemos el array de profesiones y lo preparamos para enviar
$datos['datos'] = $this->Usuarios_model->get_categorias();
$sujeto['sujeto'] = $this->Usuarios_model->get_sujeto();

// cargamos  la interfaz y le enviamos los datos
$this->load->view('frontend/usuarios',$datos);
$this->load->view('frontend/usuarios',$sujeto);

    }

model users

    public function get_categorias(){
      $query = $this->db->query('SELECT COD_MODA,NOM_MODA FROM cmi_moda');

    if ($query->num_rows()>0)
        foreach($query->result() as $row){

   $datos[($row->COD_MODA)] = $row->NOM_MODA;


}
        $query->free_result();//libera la memoria despues de usar el foreach cuando se tiene bastante data
        return $datos;



}

    public function get_sujeto(){
      $query = $this->db->query('SELECT COD_SUJE_MEDI,NOM_SUJE_MEDI FROM cmi_suje_medi');

    if ($query->num_rows()>0)
        foreach($query->result() as $row){

   $sujeto[($row->COD_SUJE_MEDI)] = $row->NOM_SUJE_MEDI;


}
        $query->free_result();//libera la memoria despues de usar el foreach cuando se tiene bastante data
        return $sujeto;

}

in the users.php view

<div class="col-md-2">
                    <select name="Categorias" class="form-control">
                        <option value="" selected="" disabled="">Elija una Opcion</option>
                    <?php

                    foreach ($sujeto as $s => $sujeto){

                       echo '<option values="',$s,'">',$sujeto,'</option>';
                     }


                    ?>
                    </select>
                    </div>

this is the other combo

<div class="col-md-2">
                    <select name="Categorias" class="form-control">
                        <option value="" selected="" disabled="">Elija una Opcion</option>
                    <?php

                    foreach ($datos as $i => $categoria){

                       echo '<option values="',$i,'">',$categoria,'</option>';
                     }
                        echo print_r($datos);
                        echo var_dump($datos);

                    ?>
                    </select>
                    </div>
    
asked by Kpeski2814 18.08.2016 в 18:27
source

1 answer

1

The problem you have is that you are loading the same view twice

public function index(){
  // $this->load->view('frontend/usuarios');
  // obtenemos el array de profesiones y lo preparamos para enviar
  $datos['datos'] = $this->Usuarios_model->get_categorias();
  $sujeto['sujeto'] = $this->Usuarios_model->get_sujeto();

  // cargamos  la interfaz y le enviamos los datos
  $this->load->view('frontend/usuarios',$datos);
  $this->load->view('frontend/usuarios',$sujeto);

 }

Remember that codeigniter to send each element of the array as a new variable, so you could create a $ data variable and send both fixes of each model, then only load the view once, and then load the two combos , something like this:

public function index(){
  // $this->load->view('frontend/usuarios');
  // obtenemos el array de profesiones y lo preparamos para enviar
  $data['datos'] = $this->Usuarios_model->get_categorias();
  $data['sujeto'] = $this->Usuarios_model->get_sujeto();

  // cargas una sola vista y enviando los datos que necesitas
  $this->load->view('frontend/usuarios',$data);

 }

And in your view loads the data of each select

<div class="form-group">
<label class="col-lg-1">Modalidad:</label>
<div class="col-md-2">
<select name="Categorias" class="form-control">
  <option value="" selected="" disabled="">Elija una Opcion</option>
  <?php
  foreach ($datos as $i => $categoria){

    echo '<option values="',$i,'">',$categoria,'</option>';
  }

  ?>
</select>
</div>

<div class="form-group">
<label class="col-lg-1">Sujeto:</label>
<div class="col-md-2">
<select name="Categorias" class="form-control">
  <option value="" selected="" disabled="">Elija una Opcion</option>
  <?php
  foreach ($sujeto as $i => $valor){

    echo '<option values="',$i,'">',$valor,'</option>';
  }

  ?>
</select>
</div>
    
answered by 18.08.2016 / 18:44
source