Welcome Guest, Not a member yet? Register   Sign In
How to get items from a table into an array for form_dropdown
#1

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
Reply
#2

(This post was last modified: 03-10-2016, 08:07 AM by keulu.)

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
Reply
#3

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?
Reply
#4

Try the form helper.

http://www.codeigniter.com/userguide3/he...set_select

Best wishes,

Paul.
Reply
#5

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
Reply
#6

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB