TAGS :Viewed: 2 - Published at: a few seconds ago

[ very simple form_dropdown codeigniter not working ]

This code is taken from CI user guide :

$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',

echo form_dropdown('shirts', $options, 'large');

// Would produce:

<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>

Looking from the code above, this code should be fine (taken from controller):

$data_search = array('kelas' => 'Kelas', 
                     'nama' => 'Nama',
                     'alamat' => 'Alamat',
                     'bulan' => 'Bulan Lahir');

In my html(view) :

<?php echo form_dropdown('ddl_search', $data_search, 'id="ddl_search"');?>

But the fact is its give me the undifined variable:data_search error, could you explain whats going on here? Thanks for your time :D

Answer 1

<?php echo form_dropdown('ddl_search', $data_search,'', 'id="ddl_search"');?>

Just looked again, you're trying to pass the id as the default value and of course that doesn't exist in your array.

Order goes like this


You can leave them off the end, you can't leave them out of the middle. Meaning you could leave off the additional parameters, you can't leave out the default value and add the additional parameters. hence the '' in my code at the top.

You need to be more descriptive in your questions, on first reading I assumed it was the data_search that was undefined, but it isn't is it?

Seeing the edited question you also have a separate issue. When passing data to the view from the controller you need a container variable that holds an array of other data.


$data['data_search'] = array('kelas' => 'Kelas', 
                     'nama' => 'Nama',
                     'alamat' => 'Alamat',
                     'bulan' => 'Bulan Lahir');

Passing the data to the view as above will make $data_search available to the view. $data passes individual variables (either single or arrays) to the view, $data itself is NOT available in the view.