0

I have returned a result_array() in $categories variable and now I want to create a dropdown menu. But if I directly use the variable, all the elements of the array are going to be displayed. However I just want to display the name and keep its id as value. How do i do it?

$options = $categories;
echo form_dropdown('category', $options);

I want something like

<select name="category">
<option value="1"> ABC </option>
..... 
<option value="10"> NNNC </option>
</select>
asked Jan 25, 2013 at 0:40

4 Answers 4

5

Keys in your $options array are not actual ID's of database rows.

So your $options array looks like this:

Array(
'0' => array('id'=>'10', 'value'=>'someval1'),
'1' => array('id'=>'22', 'value'=>'someval2'),
'2' => array('id'=>'36', 'value'=>'someval3'))

To see this, put print_r($options); before echo call.

To make real dropdown you should make helper function that looks like this:

function my_form_dropdown($name, $result_array){
 $options = array();
 foreach ($result_array as $key => $value){
 $options[$value['id']] = $value['value'];
 }
 return form_dropdown($name, $options);
}
answered Jan 25, 2013 at 0:49

1 Comment

You can also create the dropdown values right in the model if $categories is coming from a query of the database. But Nikola is bang on, the last part of his answer is the way it should be done. That way you get the id as the value, which is ultimately what you want.
1

well since I was getting all the returned rows as array in $categories

Hence a print_r() on $categories gave me the output

Array
(
 [0] => Array
 (
 [id] => 24
 [name] => First
 )
 [1] => Array
 (
 [id] => 25
 [name] => Second
 )
 [2] => Array
 (
 [id] => 26
 [name] => third
 )
)

But I needed something like the code below to make the dropdown work

Array
(
 [24] => First
 [25] => Second
 [26] => third
)

SO I had do modify my code as below

$options = array();
 foreach ($categories as $category) {
 $options[$category['id']] = $category['name'];
 }
 echo form_dropdown('cat_id', $options);

Hence the HTML generated was as follows which was the markup I desired.

<select name="category">
<option value="24"> First </option>
<option value="25"> Second </option>
<option value="26"> Third </option>
</select>

And it did worked . Thanks for the answers, But after two days of head scratching, I finally did it. Sometimes it is so difficult when u are a beginner.

answered Jan 25, 2013 at 15:04

Comments

0

you can do

$options = array(
 '1' => 'ABC',
 '2' => 'NNC'
);
echo form_dropdown('category', $options);

where the key will be the value of the dropdown

answered Jan 25, 2013 at 0:44

Comments

0
$options = array_combine(
 array_column($categories, 'id'),
 array_column($categories, 'value')
);
echo form_dropdown('category', $options, '', array('class' => 'form-control'));
answered Aug 24, 2018 at 12:37

1 Comment

Write some description even though code is descriptive

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.