i'm loading my dropdown
with this and trying set an option:
<div class="form-group">
<label for="areas" class="col-lg-2 control-label">Áreas</label>
<div class="col-lg-3">
<select class="form-control" id="areasCadastradas" name="areas">
<?php
foreach($areas as $row)
{
echo '<option value="'.$row->id.'">'.$row->descricao.'</option>';
}
?>
</select>
<?php echo $record->id_area;
echo set_select('areas', $record->id_area); ?>
</div>
</div>
it is not working... i need load the dropdown and set an option.
edit: the loading it is working.. but i cant set the option
edit 2:
foreach($areas as $row)
{
echo '<option value="'.$row->id.' '.set_select('areas', $record->id, False);.'">'.$row->descricao.'</option>';
}
This code may helps you
$options = array();
foreach ($areas as $row)
{
$options[$row->id]=$row->descricao;
}
echo form_dropdown('areas', $options, $record->id);
And,i hopes you already loaded form_validation library files in your controller
$this->load->library('form_validation');
solution:
<select class="form-control" id="comarcasCadastradas" name="comarcas">
<?php
foreach($comarcas as $row)
{
if ($record->id_comarca == $row->id) {
echo '<option value="'.$row->id.'" selected>'.$row->nome.'</option>';
} else {
echo '<option value="'.$row->id.'">'.$row->nome.'</option>';
}
}
?>
I suggest using CI's Form Helper
<?php
// All the code in the <?php tags should be in the controller and then vars passed to the view.
// Putting it all in the view here for the sake of a simple answer.
$this->load->helper('form');
$options = []; //empty array to start
foreach($areas as $row)
{
$options[$row->id] = $row->descricao;
}
$dropdown_data = [
'name' => 'areas',
'options' => $options,
'selected' => $record->id_area,
'class' => "form-control",
'id' => "areasCadastradas"
];
?>
<div class="form-group">
<label for="areas" class="col-lg-2 control-label">Áreas</label>
<div class="col-lg-3">
<?php
echo form_dropdown($dropdown_data);
?>
</div>
</div>
I assume that $record->id_area
is an array appropriate for your dropdown.