首页 文章

下拉列表中的数据从Codeigniter中的DB检索

提问于
浏览
0

在我的项目中创建下拉列表遇到问题(错误) . 我对codeigniter很新 .

请检查并告知我的代码有什么问题?

Model

Public function get_region()
{
    $return = array();
    $query  = $this->db->get('region')->result_array();
    if( is_array( $query ) && count( $query ) > 0 ){
    $return[''] = 'please select';
    foreach($query as $row)
    {
        $return[$row['id']] = $row['r_e_name'];
    }
}

Controller

public function index()
{
    $this->load->model('country_model');
    $data['countries'] = $this->country_model->get(false);
    $data['page_title']  = "Countries"; 
    $data['return']=$this->country_model->get_region();
    $this->template->show('countries', $data);
}

View

<tr>
    <td>
        <?php echo form_label('Region Name *', 'r_e_name'); ?>
    </td>
    <td>
        <?php echo form_dropdown('r_e_name', $return);?>
    </td>
</tr>

Errors

遇到PHP错误

严重性:注意

消息:未定义的变量:return

文件名:views / countries_add.php

行号:17


遇到PHP错误

严重性:警告

消息:为foreach()提供的参数无效

文件名:helpers / form_helper.php

行号:331

2 回答

  • 2

    模型

    function get_country() {
        $return[''] = 'please select';
        $this->db->order_by('c_e_name', 'asc'); 
        $query = $this->db->get('country'); 
        foreach($query->result_array() as $row){
            $return[$row['id']] = $row['c_e_name'];
        }
        return $return;
    }
    
    function get_region(){
        $return[''] = 'please select';
        $query  = $this->db->get('region');
        foreach($query->result_array() as $row){
            $return[$row['id']] = $row['r_e_name'];
        }
        return $return;
    }
    

    调节器

    public function index()
    {
        $this->load->model('country_model');
        $data['country'] = $this->country_model->get_country();
        $data['region']  = $this->country_model->get_region();
        $data['page_title']  = "Countries";
        $this->template->show('countries', $data);
    }
    

    视图

    <tr>
        <td><?php echo form_label('Country *', 'c_e_name'); ?></td>
        <td><?php echo form_dropdown('c_e_name', $country);?></td>
    </tr>
    <tr>
        <td><?php echo form_label('Region Name *', 'r_e_name'); ?></td>
        <td><?php echo form_dropdown('r_e_name', $region);?></td>
    </tr>
    
  • 0

    您尚未从模型返回结果集 . 刚刚放

    return $return;
    

    function get_region() 模型的末尾 .

相关问题