Show Selected text Select an option

2

Very good and tried to put in a selected control the option "Select an option" but it does not work for me, the combo is loading data from the database, I'm using the codeigniter framework, I would like someone to guide me in it to be able to load

Here the code

users_model.php

    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;



}

Part of the view where the selected views / frontend is

Users.php

<div class="form-group">
                    <label class="col-lg-1">Modalidad:</label>
                    <div class="col-md-2">
                    <select name="Categorias" class="form-control">
                    <?php

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

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

                    ?>
                    </select>
                    </div>

Driver users.php

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

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

I would like to achieve this option Choose an option that displays the option

    
asked by Kpeski2814 18.08.2016 в 17:39
source

2 answers

2

In the view place the option before the cycle and mark it as selected, you can also set it as disabled since that value is not in the database:

<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>
    
answered by 18.08.2016 / 17:46
source
0
                <div class="form-group">
                <label class="col-lg-1">Modalidad:</label>
                <div class="col-md-2">
                <select name="Categorias" class="form-control">
                    **<option value="">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>
    
answered by 18.08.2016 в 17:44