php CodeIgniter:使用数据库表数据填充选择输入/表单下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17471522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter: Populating a select input/ form dropdown with database table data
提问by Madhawa Ratnayake
I'm trying to populate a select input/ form dropdown with the data from the table.
我正在尝试使用表中的数据填充选择输入/表单下拉列表。
HTML
HTML
<select id="room_type" name="inputInfo" >
<option value="<?php echo set_value('room_type', '$data_room_type'); ?>"></option>
</select>
Model:
模型:
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;
}
Controller:
控制器:
function index() {
$this->load->view('/main/new_reservation');
$this->load->model('reservations_model');
$this->reservations_model->get_room_details($data_room_type);
}
But I received this error:
但我收到了这个错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data_room_type
Filename: controllers/reservations.php
Line Number: 8
遇到 PHP 错误
严重性:注意
消息:未定义变量:data_room_type
文件名:controllers/reservations.php
行号:8
Line number 8 is::
第 8 行是::
$this->reservations_model->get_room_details($data_room_type);
回答by ABorty
return $query->result_array(); line will populate more than one row for you.(if you have more than one record in room_type table)
返回 $query->result_array(); line 将为您填充多于一行。(如果您在 room_type 表中有多条记录)
so you need to run a foreach loop in view.for normal html
所以你需要在 view.for 普通 html 中运行一个 foreach 循环
<select name="">
<?php
foreach($room_type as $each)
{
?>
<option value="<?=$each['rt_name']?>"><?=$each['rt_name']?></option>
<?php
}
?>
</select>
use normal html otherwise you need to change the return data what your model returning.
使用普通的 html,否则您需要更改模型返回的返回数据。
May be it will help you.Please let me know if you face any problem.
也许它会帮助你。如果你遇到任何问题,请告诉我。
回答by Code L?ver
You have the syntax error means you have make something wrong. and as I can see that you have write the spelling of function wrong in model.
你有语法错误意味着你做错了什么。正如我所看到的,您在模型中写错了函数的拼写。
It is fucntion
you have write and it should be function
so change there and enjoy.
这是fucntion
你写的,它应该在function
那里发生变化并享受。
回答by som
fucntion get_room_details($data_room_type) {
typo function
it should be
错别字function
应该是
function get_room_details($data_room_type) {
$data_room_type[] = $row
. Missing semicolon
$data_room_type[] = $row
. 缺少分号
It should be $data_room_type[] = $row;
它应该是 $data_room_type[] = $row;
You can do like this
In Model : function get_room_details($data_room_type = array()) {
In Controller : $this->reservations_model->get_room_details();
你可以这样做在模型中:function get_room_details($data_room_type = array()) {
在控制器中:$this->reservations_model->get_room_details();
回答by Madhawa Ratnayake
This almost worked:
这几乎奏效了:
Model:
模型:
function pop_room_type() {
$this->db->select('rt_name')->from('room_type');
$query=$this->db->get();
return $query->result_array();
}
Controller:
控制器:
function index() {
$this->load->model('reservations_model');
$data['room_type'] = $this->reservations_model->pop_room_type();
//echo "<pre>";print_r($data['room_type']);echo"</pre>";
$this->load->view('/main/new_reservation', $data);
}
View:
看法:
<?php
//$js = 'name='$room_type'';
echo form_dropdown('room_type', $room_type);
?>
However, there's a one BIG issue whereas the "name" of all select options are the same (rt_name: as in the table's column).
但是,有一个大问题,而所有选择选项的“名称”都相同(rt_name:如表中的列)。