繁体   English   中英

Codeigniter-从一个数据库表中获取价值,并在表单提交时添加到其他数据库表中

[英]Codeigniter - Get value from one DB Table and Add to other DB table on form submit

长话短说,我正在将Fullcalendar与Codeigniter一起使用。 我正在根据事件的类别对日历中的事件进行颜色编码。

在管理控制台中,管理员可以添加事件类别并提供名称和颜色(从选择菜单中)。 十六进制值已保存到数据库。

event_categories_db

管理员添加事件时,他们会添加标题,描述,开始,结束和类别。

类别选项是事件类别中从数据库中拉出的选择菜单。

添加新事件时,我想使用事件类别名称并获取其颜色,然后将其与事件一起存储在数据库的最后一栏中,如下所示:

事件

保存事件:

我正在使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM