如何使用Codeigniter将数组插入MySQL? [英] How to insert arrays into MySQL using Codeigniter?

查看:87
本文介绍了如何使用Codeigniter将数组插入MySQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于我的项目,我想要使用 voucher_no amount插入 employee {employee_no} 和许多支持文档,因此我已创建了我的查看页面,用于描述和支持文档添加多个字段。

Regarding my project I want insert per employee{employee_no} many descriptions with voucher_no and amount, and many support document, so I have created my view page for descriptions and support document add multiple fields.

可以添加许多字段尽可能多,但我想插入这些数据到我的数据库,当我插入数据没有描述和支持文档。

So I can add many fields as many as I want but I want to insert those data into my database normally when I insert data without descriptions and support documents.

它完美地工作,但我的要求是让这段代码插入多个描述和支持文档。

It works perfectly but my requirement is to make this code to insert multiple descriptions and support documents.

是我的查看页面:字段到说明和支持文档

this is my View page : fields to descriptions and support documents

<div class="form-group">
        <div class="row colbox">
        <div class="col-sm-offset-2 col-lg-8 col-sm-8 text-left">Description</div>

        <div class="field_wrapper">
            <input type="textarea" name="descrip[]" value="" size="35px" /><input type="text" name="voucher_no[]" value="" size="7px"/><input type="text" name="price[]" value=""size="7px"/>
            <a href="javascript:void(0);" class="add_button" title="Add field"><img src="<?php echo base_url('images/add-icon.png'); ?>"/></a>
        </div></div></div>

        <div class="form-group">
        <div class="row colbox">
        <div class="col-sm-offset-2 col-lg-8 col-sm-8 text-left">SUPPORT DOCUMENT</div><br />

        <div class="field_upload">
            <input type="file" name="field_upload[]" value="" size="35px" />
            <a href="javascript:void(0);" class="add_butt" title="Add field">
            <img src="<?php echo base_url('images/add-icon.png'); ?>"/></a>
        </div></div></div>

我的控制器

  function index()
    {
        //fetch data from department and designation tables
        $data['department'] = $this->employee_model->get_department();
        $data['designation'] = $this->employee_model->get_designation();
        //$data['descrip'] = $this->employee_model->insert_descriptions();

        //set validation rules
        $this->form_validation->set_rules('employeeno', 'Employee No', 'trim|required|numeric');
        $this->form_validation->set_rules('employeename', 'Employee Name', 'trim|required|callback_alpha_only_space');
        $this->form_validation->set_rules('department', 'Department', 'callback_combo_check');
        $this->form_validation->set_rules('designation', 'Designation', 'callback_combo_check');
        $this->form_validation->set_rules('hireddate', 'Hired Date');
        $this->form_validation->set_rules('salary', 'Salary', 'required|numeric');

        $this->form_validation->set_rules('document', 'Document', 'trim');
        $this->form_validation->set_rules('descrip', 'Description', 'trim');

        if ($this->form_validation->run() == FALSE)
        {
            //fail validation
            $this->load->view('employee_view', $data);
        }
        else
        {    
            //pass validation
            $data = array(
                'employee_no' => $this->input->post('employeeno'),
                'employee_name' => $this->input->post('employeename'),
                'department_id' => $this->input->post('department'),
                'designation_id' => $this->input->post('designation'),
                'hired_date' => @date('Y-m-d', @strtotime($this->input->post('hireddate'))),
                'salary' => $this->input->post('salary'),
                'description' => $this->input->post('descrip'),
'document' => $this->input->post('filed_upload'),

            );

            //insert the form data into database
            $this->db->insert('tbl_employee', $data);

我知道我想使用 foreach loop 和支持文档,但我不知道如何使用它。

I know I want to use foreach loop for descriptons and support documents but I don't know how to use it

推荐答案

您有三个解决方案。


  1. 这将产生冗余数据,但它的直接解决方案,我不会

根据描述和文档数组循环你的插入数组(这两个都将作为数组)
,然后用于批处理插入

loop around your insert array on the basis of description and document array.(these both will be posted as array) and then use this for batch insert

$this->db->insert_batch();




  1. 您可以使用implode和convert这些数组以逗号分隔的值并插入它们的employee表。

  1. You can use implode and convert these arrays in comma separated values and insert them employee table. I will not prefer this too.

第三最好的。另外两个表描述和文档(要存储多个描述相关的数据和文档用于插入多个文档),这两个都将包含employee表的外键。一个员工条目和其他描述和文档条目。此链接可能会帮助您进行查询。

The third the best. Make two other tables description and documents (To store multiple description related data and document for inserting multiple documents) these both will contain foreign key for employee table. One employee entry and other description and document entries. This link may help you with queries.

http://www.codeigniter.com/userguide2/database/active_record.html#update

这篇关于如何使用Codeigniter将数组插入MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆