Upload form_dropdown in codeigniter

2

No controller I have this method :

<?php 
class Projetos extends CI_Controller {
public function index() 
{
    $data['dados_clientes'] = $this->clientes_model->get_clientes();

    $data['main_view'] = 'projetos/index';
    $this->load->view('layouts/main', $data);
}

How to put what is in dados_clientes in dropdown ?

View:

<div class="form-group">
        <?php echo form_label('Cliente'); ?>
        <?php 
            $data = array(
                    'class' => 'form-control',
                    'name' => 'proj_cliente',
            );
        ?>
        <?php echo form_dropdown($data);?>
</div>
    
asked by anonymous 13.06.2016 / 03:18

1 answer

0

As the documentation of the Framework CodeIgniter itself form_dropdown , I need a # key key key key in>).

Example:

$array = []; // ou $array = array();
$array['1'] => 'Item 1';
$array['2'] => 'Item 2';
$array['3'] => 'Item 3';

Because the key is the value of select , and value in>) is the text of select .

Then your model will have to return an array of that style to load the information.

Demonstration of the documentation itself:

$options = array(
        'small'         => 'Small Shirt',
        'med'           => 'Medium Shirt',
        'large'         => 'Large Shirt',
        'xlarge'        => 'Extra Large Shirt',
);

$shirts_on_sale = array('small', 'large');
echo form_dropdown('shirts', $options, 'large');

Result:

<select name="shirts">
     <option value="small">Small Shirt</option>
     <option value="med">Medium  Shirt</option>
     <option value="large" selected="selected">Large Shirt</option>
     <option value="xlarge">Extra Large Shirt</option>
</select>

Being

form_dropdown has 4 (four) faces:

  • The first is the name of your select .
  • The second is the array of information in the format explained above
  • The third is which of the items in the array is to stay in evidence, that is, selected ( selected="selected" )
  • The fourth is array of extra settings in the same style as array of information
  • Example simulation:

    Example table:

    creditos
        id
        nome
    

    Model:

    Create a method in your model that represents this code:

    public function lists()
    {   
        $this->db->select('id,nome');
        $results = $this->db->get('creditos')->result();
        $list = array();
        foreach ($results as $result) 
        {
            $list[$result->id] = $result->nome;                
        }
        return $list;
    }
    

    Upload this model to your controller:

    public function index() 
    {
        $data['dados_creditos'] = $this->credito_model->lists();
        $data['main_view'] = 'projetos/index';
        $this->load->view('layouts/main', $data);
    }
    

    In your view do

    <div class="form-group">
        <?php echo form_label('Creditos'); ?>
        <?php echo form_dropdown($dados_creditos);?>
    </div>
    

    A very simple example of loading the information contained in a database into your View .

        
    13.06.2016 / 17:55