0

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?

2
  • uhm. I read your questions 3 times and i still dont get what you are asking. Commented Aug 22, 2012 at 9:32
  • do you see the code after this paragraph "And this is the code under controller:"? That code is for text field. I want the same code in controller that will work on Drop Down List. Commented Aug 22, 2012 at 10:11

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

thank you. that works. i have some few problem with "Select" value. Since it is already defined, the set rules seems to validate it as valid value. It should be that if the user select the "Select" value it will still return an error like: The Title field is required.
updated my answer , so now if "select" is selected it will throw an error , you must choose anything between Dr to Mr & mrs
thanks again. but i'm wondering why the message is "The Title field may only contain alphabetical characters." instead of "The Title field is required". Before i'm using required|xss_clean instead of alpha in set_rules 3rd argument which of course does not work. may be there is another parameter for this.

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.