Codeigniter form_helper使数据库行成为选择菜单中的值 [英] Codeigniter form_helper getting database rows to be values in select menu
问题描述
我正在写一个表单,其中有一个选择菜单,我想从数据库中提取值,所以我认为这将是这样的东西:
我的检视
<?php
echo form_open('admin / save_content');
echo form_fieldset();
echo form_dropdown('categories',$ select_options);
echo form_submit('category_submit','Submit');
echo form_fieldset_close();
echo form_close();
?>
我的控制器
code> function add_content(){
$ data = array();
$ this-> is_logged_in();
$ this-> load-> model('category_model');
$ data ['select_options'] = $ this-> category_model-> get_all_online();
$ this-> load-> view('admin / content / add_content',$ data);
}
我的模型
public function get_all_online(){
$ this-> db-> select('*');
$ this-> db-> from('category');
$ this-> db-> where('category_online',1);
$ query = $ this-> db-> get();
return $ query-> result();
}
select_options 在下拉菜单中我遇到此错误
遇到PHP错误
严重性:4096
消息:stdClass类的对象
无法转换为字符串
文件名:helpers / form_helper.php
行号:331
您需要向下拉列表传递一个数组,其中数组键的值为POSTed,值将为显示的文本。 p>
要实现这一点,请改变你的控制器:
function add_content ){
$ data = array();
$ this-> is_logged_in();
$ this-> load-> model('category_model');
$ data ['select_options'] = $ this-> category_model-> get_all_online_select();
$ this-> load-> view('admin / content / add_content',$ data);
}
并将此函数添加到您的模型
public function get_all_online_select(){
$ this-> db-> select('id,name'); //将此更改为要使用的两个主要值
$ this-> db-> from('category');
$ this-> db-> where('category_online',1);
$ query = $ this-> db-> get();
foreach($ query-> result_array()as $ row){
$ data [$ row ['id']] = $ row ['name'];
}
return $ data;
}
应该这样做
I am writing a form, which has a select menu in it, I want the values to pulled from the database, so I thought it would be something along these lines:
My view
<?php
echo form_open('admin/save_content');
echo form_fieldset();
echo form_dropdown('categories', $select_options);
echo form_submit('category_submit', 'Submit');
echo form_fieldset_close();
echo form_close();
?>
My controller
function add_content() {
$data = array();
$this->is_logged_in();
$this->load->model('category_model');
$data['select_options'] = $this->category_model->get_all_online();
$this->load->view('admin/content/add_content', $data);
}
my model
public function get_all_online() {
$this->db->select('*');
$this->db->from('category');
$this->db->where('category_online', 1);
$query = $this->db->get();
return $query->result();
}
now when I place the $selected_options
in the form dropdown I get this error,
A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: helpers/form_helper.php
Line Number: 331
You need to pass an array to your dropdown, where the array key will be the value that is POSTed and the value will the text that is displayed.
To achieve this, change your controller like so:
function add_content() {
$data = array();
$this->is_logged_in();
$this->load->model('category_model');
$data['select_options'] = $this->category_model->get_all_online_select();
$this->load->view('admin/content/add_content', $data);
}
and add this function to your model
public function get_all_online_select() {
$this->db->select('id, name'); //change this to the two main values you want to use
$this->db->from('category');
$this->db->where('category_online', 1);
$query = $this->db->get();
foreach($query->result_array() as $row){
$data[$row['id']]=$row['name'];
}
return $data;
}
That should do the trick
这篇关于Codeigniter form_helper使数据库行成为选择菜单中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!