colombo
colombo

Reputation: 520

how to Insert Multiple data with insert_batch in codeigneitor

i want to insert records of fifty persons to the database at one time(this can be increase).when user click the save button data should be save at the database.what is the best way to do it?

Here is my controller

function month_end_data()
    {
$data_save = array(
                    'employee_id' => $emp,
                    "annual" => $annual,
                    "casual" => $casual
);
                $id = $this->monthend_mod->savemonthenddata($data_save);
                echo json_encode(array("status" => TRUE, "id" => $id));
}

Model

class monthend_mod extends CI_Model
{
    public function savemonthenddata($data_save = array())
    {
        if ($this->db->insert_batch('pay_monthend', $data_save)) {
            return true;
        } else {
            return false;
        }
    }
}

view

<button onclick="month_end_data()" class="btn btn-danger">Save</button>

Upvotes: 1

Views: 81

Answers (1)

M. Eriksson
M. Eriksson

Reputation: 13635

Try to send in a multidimensional array:

$data = array(
    array(
        'employee_id' => 'xx',
        //... more fields
    ),
    array(
        'employee_id' => 'yy',
        //... more fields
    ),
    array( 
        //... and so on
    ),
);

...and then send that to your savemonthenddata()-method.

Upvotes: 1

Related Questions