0

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

1 Answer 1

1

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']);?>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but it returns undefined variables name, option_list and value
Can you please show us the code where you load your template?
Note: it works if i use this <?php echo form_dropdown('type', $type['options'], '', $type['attributes']) ?> but of course doesn't show the selected value first. using my code above
I stored the field name in the 'name' parameter ('name' => 'type_value',). So that all field data can be managed in the controller. Also please answer my previous comment.
Yes I get your point but I found the main problem, it's on how I load my view. I forgot to pass the $type variable. Anyways I vote your answer It is useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.