[英]CodeIgniter: Array to string conversion error on a page supposed to retrieve data from a table, then edit and update
I'm trying to get records and let users of the application edit details of records and update new details. 我正在尝试获取记录,并让应用程序的用户编辑记录的详细信息并更新新的详细信息。 But when I print_r ($row->emp_name) it returns: John Smith and ($row->emp_type) returns Cachier which are correct values.
但是当我print_r($ row-> emp_name)时,它返回:John Smith和($ row-> emp_type)返回Cachier,它们是正确的值。 But why those data are not passed into the 'emp_name' and 'emp_type' inputs in the view?
但是,为什么这些数据没有传递到视图中的“ emp_name”和“ emp_type”输入中?
Second question: I also want to know how to define a value for a dropdown, to be selected on load. 第二个问题:我还想知道如何为下拉列表定义一个值,以便在加载时选择。 Note that the dropdown values has to be from a database and should be retrieved in a similar way described above.
请注意,下拉值必须来自数据库,并且应该以上述类似的方式进行检索。
View: 视图:
<?php
$js = 'id="emp_name"';
echo form_input('emp_name', $emp_name, set_value('emp_name'), $js);
?>
Controller: 控制器:
function index() {
$this->load->model('edit/default_price_model');
//$data['query'] = $this->default_price_model->get_employees_table();
$this_employee['emp_name'] = $this->default_price_model->current_cmployees();
//$temp_array=array_merge($data, $this_employee);
$this->load->view('/edit/employees', $this_employee);
}
function form_validation() {
if($this->input->post('this_employee') == "View") {
$this->populate_form();
}
else {
$this->load->library('form_validation');
$this->form_validation->set_rules('emp_name','Employee Name', 'required|min_length[3]|max_length[60]');
$this->form_validation->set_rules('emp_type','Employee Name', 'required');
$this->form_validation->set_rules('emp_description','Optional Description', 'max_length[500]');
if ($this->form_validation->run() == FALSE ) {
$this->index();
}
else {
$this->update_table();
}
}
}
function populate_form() {
$this->load->model('edit/default_price_model');
$selection = $this->input->post('select_employee');
$data['emp_name'] = $this->default_price_model->get_employees_table($selection);
$this->index();
}
Model: 模型:
function get_employees_table($selection) {
$query = $this->db->query(" SELECT emp_name, emp_type, emp_description
FROM employees
WHERE emp_name='$selection'
ORDER BY emp_name ASC
LIMIT 1");
if($query->num_rows()>0) {
foreach($query->result() as $row) {
$row->emp_name;
$row->emp_type;
$row->emp_description;
}
}
}
Your form input is just has the form values if set in set_value
not your database values it should look like this 您的表单输入只是具有表单值(如果在
set_value
设置的话),而不是您的数据库值,它应该看起来像这样
<?php
$js = 'id="emp_name"';
echo form_input('emp_name', $emp_name,
set_value('emp_name', isset($emp_name[0]->emp_name) ? $emp_name[0]->emp_name : '')
, $js);
?>
$emp_name
is what you have from the controller and has the db values $emp_name
是您从控制器获得的,并具有db值
$data['emp_name'] = $this->default_price_model->get_employees_table($selection);
One thing i have notice you haven't returned any results from the query in your model and also the foreach loop is doing nothing 我注意到一件事,您尚未从模型中的查询中返回任何结果,并且foreach循环也无济于事
function get_employees_table($selection) {
$query = $this->db->query(" SELECT emp_name, emp_type, emp_description
FROM employees
WHERE emp_name='$selection'
ORDER BY emp_name ASC
LIMIT 1");
if($query->num_rows()>0) {
return $query->result();
}else{
return 0;
}
}
Hope it helps you 希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.