Load form_dropdown into codeigniter

Asked

Viewed 2,048 times

2

In the controller 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 the 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>

1 answer

0


As is said in documentation of own Framework Codeigniter the form_dropdown, I need a array style: key and value (key and value).

Example:

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

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

Then your model will have to return an array with this style in order 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');

Upshot:

<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>

Whereas:

form_dropdown has 4 (four) vestments:

  1. The first is the name of your select.
  2. The second is the array of information in the format just explained above
  3. The third is which of the items in the array is to be in evidence, ie selected (Selected="Selected")
  4. The room is array of extra settings in the same style 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;
}

Load this model into 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>

Example, very simple to load the information contained in a database, for your View.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.