试图使用mysql数据填充codeigniter中的下拉菜单 [英] Trying to populate a dropdown menu in codeigniter with mysql data

查看:116
本文介绍了试图使用mysql数据填充codeigniter中的下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to populate a dropdown box on a registraton form in code igniter but i have had not luck below are snippets from my contorller, model, and view. Base is the drop down field.

模型

 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个基础我做了一个精彩的视图,并将其包含在主要的注册表单

View 2 Bases i made this a sperate view and included it in the main registration form

<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)
        {


推荐答案

您的格式应该是这样:

$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>

REFER 这里



UPDATE

您也可以尝试此操作

MODEL

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

CONTROLLER

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

VIEW - 您的DROPDOWN PROBLEM

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

这篇关于试图使用mysql数据填充codeigniter中的下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆