Welcome Guest, Not a member yet? Register   Sign In
Newb help: Populate Dropdown
#1

[eluser]Tumac[/eluser]
I am new to CI and the whole MVC concept, but have created some MVC structures successfully with some simple CRUD routines to make sure I understand the basic concepts.

I now am ready to put my feet to the fire and am having problems with FORM generation and passing look-up values from tables to a form as a series of drop-downs (no dependence on each other).

My lookup tables are pretty basic: ID, VALUE

In my form, I want my dropdowns to display the VALUE, but when I submit the form, I want the ID stored in the database table.

Can someone point me in the right direction to getting multiple dropdowns populated? This is just one dropdown I am trying to populate. Once I get one, I need to get 10 others from similar tables....

Code:
//Controller

function newPO() {
        $this->load->model('lookup_model');
        $data['cusname']=$this->lookup_model->getCusname();
        //$this->load->view('po/po_entry');
        $this->load->view('po_entry',$data);
    }


//Model
function getCusname () {
        
        $this->db->select('CusID','CusName');
        $this->db->where('active',"Y");
        $records=$this->db->get('lu_customer_vendor');
        
        $data=array();
        
        foreach($records as $rec) {
            $data[$rec->cusID]=$rec->CusName;
        }
        
        return ($data);
    }
#2

[eluser]theprodigy[/eluser]
The first thing you want to do is change your foreach statement in your model
Code:
foreach($records as $rec) {
    $data[$rec->cusID]=$rec->CusName;
}

You want to iterate over the results of the data object. Change it to
Code:
foreach($records->result() as $rec) {
    $data[$rec->cusID]=$rec->CusName;
}


You can find more information about accessing database query results HERE

To populate the drop down, you want to use the form helper function called form_dropdown()
#3

[eluser]Tumac[/eluser]
Thanks - that worked - I had my view correct - wasn't passing the query properly. Can I now pass multiple results like this in the same way?

ie:

Code:
function newPO() {
        $this->load->model('lookup_model');
        $data['cusname']=$this->lookup_model->getCusname();

        $data2['orig']=$this->lookup_model->getOrigin();

        $this->load->view('po_entry',array($data,data2,etc.....);
        
    }
#4

[eluser]theprodigy[/eluser]
Change
Code:
function newPO() {
        $this->load->model('lookup_model');
        $data['cusname']=$this->lookup_model->getCusname();

        $data2['orig']=$this->lookup_model->getOrigin();

        $this->load->view('po_entry',array($data,data2,etc.....);
        
    }
to
Code:
function newPO() {
    $this->load->model('lookup_model');
    $data['cusname']=$this->lookup_model->getCusname();

    $data['orig']=$this->lookup_model->getOrigin();

    $this->load->view('po_entry',$data);
}
You only need one array. In your view, you pass in $cusname to the one drop down, and $orig to the other drop down.
#5

[eluser]Unknown[/eluser]
Hi, I have a question,

How do you set the second drop down to have a where clause depending on the selection of the first drop down?
#6

[eluser]theprodigy[/eluser]
You will have to add an onChange attribute to your first drop down box

You can pass extra info to your select drop down via the 4th parameter of the form_dropdown function

Code:
$js = 'onChange="some_function();"';

echo form_dropdown('shirts', $options, 'large', $js);

You will need to either set that function to do a page refresh (and go to a different page)
OR
Use javascript to populate the second dropdown via what gets returned from an AJAX call.

I personally prefer the second method as it's more what the user would expect.




Theme © iAndrew 2016 - Forum software by © MyBB