Welcome Guest, Not a member yet? Register   Sign In
form_dropdown with mysql results
#1

[eluser]petroz[/eluser]
Hi Guys,

I am a lil new to codeigniter, but so far I love it.

I am getting hung up on dynamic form_dropdowns. I did some searching but I cant seem to find the answer I am looking for... There is a option label showing up which I dont want... also, I am unable to define the selected option.

Any help would be greatly appreciated. I have attached my code and results.

First, my model. I query the database for the option value (bid) then the option (brand_name). I am guess my problem lies here...
Code:
//this one gets all the possible options
function get_brand_bid(){

        $this->db->select('bid, brand_name');
        $this->db->where('status = 0');
        $query = $this->db->get('brands');
        if ($query->num_rows() > 0){
             foreach ($query->result_array() as $row){
                 $data[] = array(
                     $row['bid'] => $row['brand_name']
                    );    
            }
        
        }        
    
    $query->free_result();
    return $data;

}    
//this is supposed to get the selected option
function get_brand($sid){
    
        $this->db->select('`brands`.`bid`, `brands`.`brand_name`');
        $this->db->where('`styles`.`sid`', $sid);
        $this->db->where('`styles`.`bid` = `brands`.`bid`');
        $query = $this->db->get('`styles`, `brands`');
        if ($query->num_rows() > 0){
             foreach ($query->result_array() as $row){
                 $data = array(
                     $row['bid'] => $row['brand_name']
                    );    
            }
        
        }        
    
    $query->free_result();
    return $data;
    
    }

    
    }

My controller (simplified)
Code:
//get all brands
$data['brands'] = $this->brands_model->get_brand_bid();

//get selected brand
$data['brand'] = $this->brands_model->get_brand($sid);

//load the view
$data['main_content'] = 'inventory/list_inventory';
$this->load->view('includes/template', $data);

My View (simplified)
Code:
echo form_label('Brand');            
echo form_dropdown('brand',$brands, $brand);

I have attached an image showing what the drop down looks like as well.
#2

[eluser]Eric Barnes[/eluser]
Your returned array needs to look like this:
Code:
<?php $options = array('y' => lang('lang_yes'), 'n' => lang('lang_no')); ?>
<?php echo form_label('Closed:', 'status_closed'); ?>
<?php echo form_dropdown('status_closed', $options, set_value('status_closed'), 'id="status_closed"'); ?>

So I would think something like this should work:
Code:
if ($query->num_rows() > 0)
{
    $data = array();
    foreach ($query->result_array() as $row)
    {
        $data[$row['bid']] = $row['brand_name'];
    }
}
#3

[eluser]petroz[/eluser]
Worked like a charm! Thanks Eric!




Theme © iAndrew 2016 - Forum software by © MyBB