php - Codeigniter foreach 循环行插入到表中
问题描述
我在前端有一个表单,我试图将值提取到隐藏的输入元素中,然后尝试插入到表中。但它不起作用,我收到未定义变量的错误,user_id_ins
我正在从函数中将值提取到隐藏的输入元素中get_autocomplete
。
你们能帮我实现这一目标吗?
控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class AddMeeting extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->helper('form');
$this->load->database();
$this->load->model('meetinginsert');
}
function index() {
$this->form_validation->set_rules('organisationName', 'Organisation name', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('addmeeting');
} else {
$data = array(
'organisation_name' => $this->input->post('organisationName'),
'user_id' => $this->input->post('user_id')
);
$this->meetinginsert->meeting_insert($data);
$data['message'] = 'Meeting added';
$this->load->view('addmeeting', $data);
}
}
function get_autocomplete() {
if(isset($_GET['term'])) {
$result = $this->meetinginsert->search_user($_GET['term']);
if(count($result) > 0) {
foreach($result as $row) {
$user_id_ins = $row->id;
$arr_result[] = $row->first_name . " " . $row->last_name;
echo json_encode($arr_result);
}
}
}
}
}
?>
看法
<input type="hidden" name="user_id" value="<?php echo $user_id_ins; ?>">
解决方案
假设它$arr_result
是您输入视图的内容,错误是因为$user_id_ins
不包含在视图中$arr_result
,因此没有传递给视图。
$arr_result
,您现在拥有的方式只是一个一维数组,其中每个元素都只是姓名和姓氏的串联。您需要传递两个参数,例如:
$arr_result[] = (user_id_ins => $user_id, fullname => $row->first_name . " " . $row->last_name);
然后,您可以通过调用$user_id_ins
和$fullname
(或您愿意使用的任何名称)访问视图中的两个字段
推荐阅读
- php - Internet Explorer 11 向 Laravel 后端发送格式不正确的日期
- javascript - 输入类型=时间显示错误的时区
- javascript - react-naitve 组件“RNCamera”重新注册冒泡事件“topPictureTaken”作为直接事件模块ConstantsForComponent
- java - maven cxf-codegen-plugin XMLStreamException 出错:属性限制
- python - 如何将循环输出保存到数据框中的列
- javascript - 一个输入组件,但每次出现的状态不同
- c - XOR 神经网络似乎收敛在 0.5 左右
- ruby-on-rails - rails 6 中的资产问题,用于葡萄大摇大摆的轨道
- python - 带有 CascadeClassifier 的 Python OpenCV 错误
- java - 如何在 Selenium 中将 LI 与 UL 分开