Hamelraj
Hamelraj

Reputation: 4826

How to insert arrays into MySQL using Codeigniter?

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>

my controller :

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

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

Upvotes: 3

Views: 5546

Answers (3)

NealH
NealH

Reputation: 21

You could get a count of the records, then use a for loop to create the data array to insert into the database.

$records = count($_POST['employee_no'];

for($i=0; $i<$records; $i++) {
    $data = array(
        'employee_no' => $_POST['employeeno'][$i],
        'employee_name' => $_POST['employeename'][$i]
        ..etc
    );

    $this->db->insert('tbl_employee', $data);
}

Upvotes: 1

Harish Lalwani
Harish Lalwani

Reputation: 774

you have three solutions for your problem.

  1. This will generate a redundant data but its the straight solution, I will not prefer that.

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. You can use implode and convert these arrays in comma separated values and insert them employee table. I will not prefer this too.

  2. 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

Upvotes: 1

Nithin Krishnan P
Nithin Krishnan P

Reputation: 758

You can use insert_batch For Eg:

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

$this->db->insert_batch('mytable', $data); 

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

Upvotes: 1

Related Questions