claudios
claudios

Reputation: 6656

Fetch and display dropdown selected value in codeigniter

I wanted to fetch the selected value from the database and display it in codeigniter form_dropdown() function but it displays wrong.

Controller:

$type = array(
'options' => array(
  'section'       => 'Section',
  'transaction' => 'Transaction',
  'document'      => 'Document'
  ),
'attributes' => array(
  'class' => 'form-control'
  )
);

View:

<?php echo form_dropdown('type', $type['options'],'', $type['attributes']) ?>

The Screenshot

Upvotes: 0

Views: 1814

Answers (1)

Sankar V
Sankar V

Reputation: 4128

Try the below code:

Controller:

$this->data['type']  =  array(
        'name'         => 'type_value',
        'attributes'   => 'class="form-control"',
        'value'        => (isset($database_type_value) && trim($database_type_value)) ? $database_type_value: $this->input->post('type_value',TRUE),   //$database_type_value - value from database
        'options_list' => array(
            'section'        => 'Section',
            'transaction'    => 'Transaction',
            'document'       => 'Document'
        ),
);

View:

<?php echo form_dropdown($type['name'],$type['options_list'],$type['value'],$type['attributes']);?>

Upvotes: 1

Related Questions