CodeIgniter:使用数据库表数据填充选择输入/表单下拉列表 [英] CodeIgniter: Populating a select input/ form dropdown with database table data

查看:79
本文介绍了CodeIgniter:使用数据库表数据填充选择输入/表单下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用表中的数据填充选择输入/表单下拉列表.

I'm trying to populate a select input/ form dropdown with the data from the table.

HTML

<select id="room_type" name="inputInfo" >
    <option value="<?php echo set_value('room_type', '$data_room_type'); ?>"></option>
</select>

型号:

function get_room_details($data_room_type) { // line 19
    $data_room_type = array();
    $this->db->select('room_type', 'default_price');
    $this->db->from('room_type');
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        foreach ($query->result_array() as $row) {
            $data_room_type[] = $row;
        }
    }
    return $data_room_type;
}

控制器:

function index() {
    $this->load->view('/main/new_reservation');
    $this->load->model('reservations_model');
    $this->reservations_model->get_room_details($data_room_type);
}

但是我收到此错误:

遇到PHP错误

A PHP Error was encountered

严重性:通知

消息:未定义的变量:data_room_type

Message: Undefined variable: data_room_type

文件名:controllers/reservations.php

Filename: controllers/reservations.php

行号:8

第8行是::

$this->reservations_model->get_room_details($data_room_type);

推荐答案

return $ query-> result_array();行将为您填充多行.(如果room_type表中有多条记录)

return $query->result_array(); line will populate more than one row for you.(if you have more than one record in room_type table)

因此您需要在view.中运行foreach循环.对于普通的html

so you need to run a foreach loop in view.for normal html

<select name="">
<?php
foreach($room_type as $each)
{
    ?>
    <option value="<?=$each['rt_name']?>"><?=$each['rt_name']?></option>
    <?php
}
?>
</select>

使用普通的html,否则您需要更改模型返回的返回数据.

use normal html otherwise you need to change the return data what your model returning.

也许会对您有帮助.如果您遇到任何问题,请告诉我.

May be it will help you.Please let me know if you face any problem.

这篇关于CodeIgniter:使用数据库表数据填充选择输入/表单下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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