Welcome Guest, Not a member yet? Register   Sign In
A little lost on Active Record and Form Helper
#1

[eluser]Unknown[/eluser]
Hello,

I'm having a bit of a challenge with ci and would appreciate your input. I'm not a php guru but I can write some php code.

I want to populate a dropdown menu with some values from a db

blue shirt ->blue shirt id
red shirt ->red shirt id

I'm using the form helper that expects an assoc array.

The problem is, how do I get the values from the database to be in the form that the form help wants?

My db query is in function get_shirts():
Code:
$rows = $this->db->get('shirts');

The value I'm passing to the view page is:
Code:
$data['options'] = $this->get_shirts():

On my view page I have:
Code:
echo form_dropdown('shirts', $options, 'large');

:bug:

thank you
#2

[eluser]GSV Sleeper Service[/eluser]
normally I will pass over an array that looks something like this to form_dropdown()
Code:
$data[options] = array(1=>'red shirt',2=>'blue shirt',3=>'pink shirt')
in your case you'll have to do this
Code:
function get_shirts()
{
    $out = array();
    $query = $this->db->get('shirts');
    foreach ($query->result() as $row)
    {
        $out[$row->id] = $row->name;//change according to your db
    }
    return $out
}
and the rest of your code should work as expected.
#3

[eluser]Unknown[/eluser]
thanks mate, that sorted me out!
Appreciate the help




Theme © iAndrew 2016 - Forum software by © MyBB