0

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> </select> 

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

3
  • Are you defining data_search in the controller or view? Commented Apr 6, 2013 at 11:35
  • Yes, i add it in the snippet of my code (the middle one) Commented Apr 6, 2013 at 11:57
  • Yes isn't an answer, WHERE is that code? In the controller or the view itself? Is it saying data_search is undefined? Showing code is great but small snippets without explaining where they are makes it difficult. Commented Apr 6, 2013 at 12:36

1 Answer 1

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

form_dropdown('nameOfControl',$dataToPopulateControl,'defaultValueOfControl','additionalParameters'); 

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.

Controller:

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

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.

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

1 Comment

Yes, i forget to add the data[]. Thanks for your help :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.