Php Codeigniter-从一个DB表中获取值,并添加到表单submit上的另一个DB表中
长话短说,我正在使用带有Codeigniter的Fullcalendar。我正在根据事件的类别对日历中的事件进行颜色编码 在管理仪表板中,管理员可以添加事件类别并提供名称和颜色(从选择菜单)。十六进制值保存到数据库 当管理员添加事件时,他们会添加标题、描述、开始、结束和类别 类别选项是从数据库中提取的事件类别的选择菜单 添加新事件时,我希望使用事件类别名称并获取其颜色,然后将其与最后一列中的事件一起存储在数据库中,如下所示: 保存事件: 我正在使用codeigniter表单验证,如果所有字段都已验证,我将尝试从事件类别表中获取颜色,并将其添加到$save_数据数组中的事件:Php Codeigniter-从一个DB表中获取值,并添加到表单submit上的另一个DB表中,php,database,forms,codeigniter,Php,Database,Forms,Codeigniter,长话短说,我正在使用带有Codeigniter的Fullcalendar。我正在根据事件的类别对日历中的事件进行颜色编码 在管理仪表板中,管理员可以添加事件类别并提供名称和颜色(从选择菜单)。十六进制值保存到数据库 当管理员添加事件时,他们会添加标题、描述、开始、结束和类别 类别选项是从数据库中提取的事件类别的选择菜单 添加新事件时,我希望使用事件类别名称并获取其颜色,然后将其与最后一列中的事件一起存储在数据库中,如下所示: 保存事件: 我正在使用codeigniter表单验证,如果所有字段
public function add_save()
{
$this->form_validation->set_rules('title', 'Title', 'trim|required|max_length[500]');
$this->form_validation->set_rules('start', 'Start', 'trim|required');
$this->form_validation->set_rules('end', 'End', 'trim|required');
$this->form_validation->set_rules('description', 'Description', 'trim|required|max_length[1000]');
$this->form_validation->set_rules('category', 'Category', 'trim|required|max_length[100]');
$this->form_validation->set_rules('has_attendance', 'Has Attendance', 'trim|max_length[1]');
$this->form_validation->set_rules('is_recurring', 'Is Recurring', 'trim|required|max_length[1]');
if ($this->form_validation->run()) {
// I am adding this to capture color from event_category table
// 1. use the input category field from event
// 2. then I select all from event_category table
// 3. WHERE name is equal to the selected category name from input
// 4. The color is the reulting rows color field
$selected_event_category = $this->input->post('category');
$this->db->get('event_category');
$this->db->where('name',$selected_event_category);
$the_color = $this->db->get()->result()->row('color');
$save_data = [
'title' => $this->input->post('title'),
'start' => $this->input->post('start'),
'end' => $this->input->post('end'),
'description' => $this->input->post('description'),
'category' => $this->input->post('category'),
'has_attendance' => $this->input->post('has_attendance'),
'is_recurring' => $this->input->post('is_recurring'),
'color' => $the_color //I have added this from above query
];
$save_events = $this->model_events->store($save_data);
} else {
$this->data['success'] = false;
$this->data['message'] = validation_errors();
}
echo json_encode($this->data);
}
我尝试过进行查询,并将结果存储在名为$theu color的变量中。然后,我在$save_数据数组中使用此变量作为颜色值
但是表格不会发布,我也没有收到任何错误。事件将不会保存,它根本不会进入数据库
我希望有人能帮我指出哪里出了问题?这个怎么样?如果希望从数据库中得到一条记录,我认为可以使用row()方法。此外,在存储数据时,不必将其分配给变量 模型文件中的方法:
public function getEventCategory($selected_event_category) {
$this->db->where('name', $selected_event_category);
$q = $this->db->get('event_category');
$q = $q->row();
return $q;
}
然后在控制器中
if ($this->form_validation->run()) {
// I am adding this to capture color from event_category table
// 1. use the input category field from event
// 2. then I select all from event_category table
// 3. WHERE name is equal to the selected category name from input
// 4. The color is the reulting rows color field
$selected_event_category = $this->input->post('category');
$event_category = $this->Your_model_here->getEventCategory($selected_event_categor);
$the_color = $event_category->color;
$save_data = [
'title' => $this->input->post('title'),
'start' => $this->input->post('start'),
'end' => $this->input->post('end'),
'description' => $this->input->post('description'),
'category' => $this->input->post('category'),
'has_attendance' => $this->input->post('has_attendance'),
'is_recurring' => $this->input->post('is_recurring'),
'color' => $the_color //I have added this from above query
];
$this->model_events->store($save_data);
} else {
$this->data['success'] = false;
$this->data['message'] = validation_errors();
}
echo json_encode($this->data);
}
另一个问题是您应该将查询传递给模型。Codeigniter基于MVC模型,因此我们应该避免在控制器中使用查询。嘿,KamS,它工作得很好!非常感谢。另外,关于将其放入模型中,我完全知道您的意思,但我不知道如何从模型中执行:$selected_event_category=$this->input->post('category')@BrianRevie我已经找到了答案。我已经为一个模型添加了一个方法,然后以一种完全不可思议的可控方式接收它!工作完美!非常感谢。我学到了一些东西,将变量传递给函数!我非常感谢你的支持!