jaypabs
jaypabs

Reputation: 1567

How to put drop down list into array using codeigniter

I use the ion auth for codeigniter and it works great except that I don't know how to code drop down list the same way as the text field.

If you want to display a text field under view you have to issue this:

<?php echo form_input($first_name);?>

And this is the code under controller:

$this->data['first_name'] = array(
    'name'      => 'first_name',
    'id'        => 'first_name',
    'type'      => 'text',
    'size'      => 32,
    'maxlength' => 32,
    'value' => $this->form_validation->set_value('first_name'),
);

But how can I put the value of my drop down list into an array?

I tried putting this code under view:

<?php
$options = array(
                  ''    => 'Select',
                  'Dr'  => 'Dr.',
                  'Mr'  => 'Mr.',
                  'Mrs' => 'Mrs.',
                  'Ms' => 'Ms.',
                  'Prof' => 'Prof.',
                  'Mr. & Mrs.' => 'Mr. & Mrs.',
                );
echo form_dropdown('title', $options, '$title');
?>

I am thinking that under view I should code it like: echo form_dropdown($title); the same as text field but what is the code under controller?

Upvotes: 0

Views: 2612

Answers (1)

Nishant Jani
Nishant Jani

Reputation: 1993

First in your controller you can do :

$options = array(
              '0'    => 'Select',
              'Dr'  => 'Dr.',
              'Mr'  => 'Mr.',
              'Mrs' => 'Mrs.',
              'Ms' => 'Ms.',
              'Prof' => 'Prof.',
              'Mr. & Mrs.' => 'Mr. & Mrs.',
            );
$data['options'] = $options
$this->load->view('your_view',$data);

In the View :

echo form_dropdown('title', $options, set_value('title'));

Moreover think you want to retain the value of the dropdown if a form validation error occurs:

you just need to put some validation to retain the values of the dropdown , somethin like:

$this->form_validation->set_rules('title','Titles','alpha');

If not validatted the dropdown will not retain the values in spite of using set_value

Upvotes: 1

Related Questions