[英]Codeigniter - Get value from one DB Table and Add to other DB table on form submit
長話短說,我正在將Fullcalendar與Codeigniter一起使用。 我正在根據事件的類別對日歷中的事件進行顏色編碼。
在管理控制台中,管理員可以添加事件類別並提供名稱和顏色(從選擇菜單中)。 十六進制值已保存到數據庫。
管理員添加事件時,他們會添加標題,描述,開始,結束和類別。
類別選項是事件類別中從數據庫中拉出的選擇菜單。
添加新事件時,我想使用事件類別名稱並獲取其顏色,然后將其與事件一起存儲在數據庫的最后一欄中,如下所示:
保存事件:
我正在使用codeigniter表單驗證,並且如果所有字段都經過驗證,則嘗試從事件類別表中獲取顏色並將其添加到$ save_data數組中的事件中:
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);
}
我嘗試執行查詢並將結果存儲在名為$ the_color的變量中。 然后,我在$ save_data數組中使用此變量作為顏色值。
但是表格不會發布,我也沒有收到任何錯誤。 該事件將不會保存,它根本不會進入數據庫。
我希望有人可以幫我指出我要去哪里錯了?
這個怎么樣? 我認為如果期望數據庫中有一條記錄,則可以使用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模型,因此我們應避免在控制器中使用查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.