CodeIgniter Forums
How to get items from a table into an array for form_dropdown - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to get items from a table into an array for form_dropdown (/showthread.php?tid=64595)



How to get items from a table into an array for form_dropdown - doomie22 - 03-10-2016

Hi all,

I am trying to figure out how I can pull items from a database into the array for form_dropdown.  Looking at the docs I see this:

PHP Code:
$options = array(
 
       'small'         => 'Small Shirt',
 
       'med'           => 'Medium Shirt',
 
       'large'         => 'Large Shirt',
 
       'xlarge'        => 'Extra Large Shirt',
);

$shirts_on_sale = array('small''large');
echo 
form_dropdown('shirts'$options'large'); 

In the controller I am pulling the data in:

PHP Code:
$data['categories'] = $this->Admin_model->get_getcategories('blog_categories'); 

I was trying to put a foreach loop into it but it just errors before I even try.  I am after 'name' to be the thing that you see in the dropdown box with the value 'url'. Can anyone shed any light on how to even start to make this?

Thanks,

Doomie


RE: How to get items from a table into an array for form_dropdown - keulu - 03-10-2016

PHP Code:
$options = array(
        
'small'         => 'Small Shirt',
        
'med'           => 'Medium Shirt',
        
'large'         => 'Large Shirt',
        
'xlarge'        => 'Extra Large Shirt',
); 

is an array

all return from database is object...

you need to build your own form_dropdown data like this

PHP Code:
$categories_db $this->Admin_model->get_getcategories('blog_categories'); 
$categories_ar = array();
foreach(
$categories_db as $category){
    
$categories_ar[$category->id] = $category->name;
}
$data['categories'] = $categories_ar

of course, check if you have categories from your DB (!= false) before your foreach


RE: How to get items from a table into an array for form_dropdown - doomie22 - 03-10-2016

Thank you for that, I am just curious now on how to to use a select within this? as I want it to show what they selected when they edit in the edit form?


RE: How to get items from a table into an array for form_dropdown - PaulD - 03-10-2016

Try the form helper.

http://www.codeigniter.com/userguide3/helpers/form_helper.html#set_select

Best wishes,

Paul.


RE: How to get items from a table into an array for form_dropdown - doomie22 - 03-11-2016

I have been trying to figure out how to use the set_select() but I am not getting any response from it.  I have tried to hard code it to make it move but nothing.

PHP Code:
<?php echo form_dropdown(array('id' => '1''name' => 'category''class' => 'form-control''value' => ''), $categoriesset_select('category''News')); ?>

Doomie


RE: How to get items from a table into an array for form_dropdown - Wouter60 - 03-11-2016

Set_select() is only useful if you want to use the form_validation class. Otherwise, you can populate the selected value by setting the 'selected'=>... attribute in your array.