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:
- The first is the name of your select.
- The second is the array of information in the format just explained above
- The third is which of the items in the array is to be in evidence, ie selected (Selected="Selected")
- 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.