【问题标题】:Trying to populate a dropdown menu in codeigniter with mysql data尝试使用 mysql 数据填充 codeigniter 中的下拉菜单
【发布时间】:2013-06-07 06:12:23
【问题描述】:

我试图在代码点火器中的注册表单上填充一个下拉框,但我没有运气,下面是来自我的控制器、模型和视图的 sn-ps。 Base 是下拉字段。

型号

 function get_airports()
    {
        $this->db->select('airport_code, airport_name');
        $this->db->order_by('airport_code', "asc");
        $query = $this->db->get('airports');
    if($query){
        $query = $query->result_array();
        return $query;
    }
    }

    }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;
    }

} ?>

查看

    $airports = $this->get_airports(true,'Please Select');
$selected = set_my_defaults('airport',$default);

echo form_dropdown('base', set_value('base', $airports, $selected, 'class="dropdown_style"'));

查看 2 个基地,我将其作为一个单独的视图并将其包含在主注册表中

<select>
<?php 
    foreach($myDropdown as $dd)
        echo "<option value='". $dd['your_field'] ."'>". $dd['your_field'] ."</option>";
?>
</select>

控制器

function getairport(){
$query = $this->membership_model->get_airports();
if($query)
{
    $data['main_content'] = 'base';
    $this->load->view('base', $data);
}
}
        function create_member()
    {
        $this->load->library('form_validation');
        //fieldname,  error messaage, validation rules
        $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email_address', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
        $this->form_validation->set_rules('birthdate', 'Birthdate', 'trim|required|');
        $this->form_validation->set_rules('base', 'Base', 'trim|required');

        if($this->form_validation->run()== FALSE)
        {

【问题讨论】:

    标签: php mysql codeigniter drop-down-menu codeigniter-form-helper


    【解决方案1】:

    你的格式应该是这样的:

    $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');
    

    这将产生:

    <select name="shirts">
        <option value="small">Small Shirt</option>
        <option value="med">Medium Shirt</option>
        <option value="large" selected="selected">Large Shirt</option>
        <option value="xlarge">Extra Large Shirt</option>
    </select>
    

    推荐HERE


    更新

    如果你愿意,你也可以试试这个

    型号

    $this->db->select('airport_code, airport_name');
    $this->db->order_by('airport_code', "asc");
    $query = $this->db->get('airports');
    if($query)
    {
        $query = $query->result_array();
        return $query;
    }
    

    控制器

    $query = $this->your_model->your_method();
    if($query)
    {
        $data['myDropdown'] = $query;
        $this->load->view('your_view', $data);
    }
    

    查看 - 您的下拉问题

    <select>
    <?php 
        foreach($myDropdown as $dd)
            echo "<option value='". $dd['your_field'] ."'>". $dd['your_field'] ."</option>";
    ?>
    </select>
    

    【讨论】:

    • 我的数据不是写在代码里的,来自一个sql数据库
    • 您能展示一下您的$airports 的样子吗? print_r($airports)
    • 当我尝试在第 9 行第 9 行调用未定义方法 CI_Loader::get_airports() $airports = $this->get_airports(true,'Please Select');
    • 应该只是$this-&gt;db-&gt;get('airports'); 吗?
    • @KobusMyburgh 不,它有,见[num_rows]=&gt;5 ?结果是这样的,因为也许他没有做出-&gt;result_array
    猜你喜欢
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 2011-08-17
    • 2013-04-18
    相关资源
    最近更新 更多