简体   繁体   English

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

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

Long story short, I am using Fullcalendar with Codeigniter. 长话短说,我正在将Fullcalendar与Codeigniter一起使用。 I am color coding the events in the calendar depending on the category of the event. 我正在根据事件的类别对日历中的事件进行颜色编码。

In Admin dashboard admin can add event category and provide name and color (from select menu). 在管理控制台中,管理员可以添加事件类别并提供名称和颜色(从选择菜单中)。 Hex value gets saved to database. 十六进制值已保存到数据库。

event_categories_db

When Admin adds an event, they add title, description, start, end and category. 管理员添加事件时,他们会添加标题,描述,开始,结束和类别。

Category option is a select menu from Event Categories, pulled from database. 类别选项是事件类别中从数据库中拉出的选择菜单。

When I add a new event, I want to use the event category name and grab its color and then store it with the event in the database in the last column like so: 添加新事件时,我想使用事件类别名称并获取其颜色,然后将其与事件一起存储在数据库的最后一栏中,如下所示:

事件

Saving an Event: 保存事件:

I am using codeigniter form validation, and if all fields are validated I am trying to grab the color from the event category table and add it to the event in my $save_data array: 我正在使用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);
}

I have tried to do the query and store the result in a variable called $the_color. 我尝试执行查询并将结果存储在名为$ the_color的变量中。 I am then using this variable in my $save_data array as the color value. 然后,我在$ save_data数组中使用此变量作为颜色值。

But the form will not post and I am not getting any errors. 但是表格不会发布,我也没有收到任何错误。 The event will not save, it does not go into the database at all. 该事件将不会保存,它根本不会进入数据库。

I am hoping someone could possibly help point out where I am going wrong? 我希望有人可以帮我指出我要去哪里错了?

How about this? 这个怎么样? I think you can use the row() method if you expect a single record from the database. 我认为如果期望数据库中有一条记录,则可以使用row()方法。 Moreover, when you store data, you don't have to assign it to a variable. 而且,当您存储数据时,不必将其分配给变量。

Method in model file: 模型文件中的方法:

public function getEventCategory($selected_event_category) {
    $this->db->where('name', $selected_event_category);
    $q = $this->db->get('event_category');
    $q = $q->row();

    return $q;
}

And then in controller 然后在控制器中

    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);
    }

Another issue is you should pass your query to model. 另一个问题是您应该将查询传递给模型。 Codeigniter is base on the MVC model, so we should avoid using queries in the controller. Codeigniter基于MVC模型,因此我们应避免在控制器中使用查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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