表单助手下拉菜单

时间:2013-06-14 20:45:01

标签: php sql codeigniter codeigniter-form-helper

有没有办法使用CodeIgniter中的表单助手创建MySQL链接下拉菜单?我试图这样做,但我似乎无法让它工作。

模型

function get_airports()
    {
        $this->db->select('airport_code, airport_name');
        $this->db->order_by('airport_code', "asc");
        $query = $this->db->get('airports');
    foreach ($query->result_array() as $row){
        $data{$row['airport_code']} = $row['status'];
    }

    }return $data;
    }
    function create_member()
    {
        $new_member_insert_data = array('first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'), 
            'email_address'=> $this->input->post('email_address'),
            'username'=> $this->input->post('username'),
            'password'=> md5($this->input->post('password'))
            'birthdate'=> $this->input->post('birthdate'),
            'base'=> $this->input->post('base')
        );
        $insert = $this->db->insert('membership', $new_member_insert_data);
        return $insert;
    }

} ?>

查看

echo form_dropdown('base', set_value('base', 'Select a Base'));

控制器

$this->form_validation->set_rules('base', 'Base', 'trim|required');

1 个答案:

答案 0 :(得分:0)

一种方法是让你的下拉列表如下:

<?PHP 
                $options    =   $this->db->query('Select province_id as value, province as display FROM ndi_provinces')->result();
                $prov_options = array(''=>'Select');
                foreach($options as $option){
                    $prov_options[$option->value] = $option->display;
                }
                $prov_optionAtt = "id='province_id'  readonly='readonly'";
                echo form_dropdown('province_id',$prov_options, isset($_POST['province_id'])?$_POST['province_id']:$participant->province_id, $prov_optionAtt);
            ?><span class="required">*</span>

另一种更好的方法是在控制器中创建下拉选项 并将其作为变量发送到视图