如何在Codeigneitor中同时插入多个记录到数据库中 [英] How to insert multiple records to database in Codeigneitor at same time
问题描述
我想插入一些值到数据库,我有这些值通过做一些计算。现在我可以在html报告中查看这些值。但我想做的是将这些多个值插入数据库。有50个人。我想通过点击按钮同时添加这50个人的所有细节。我可以这样做吗?可以是我的控制器视图和模型。
I want to insert some values to the database where i have got those values by doing some calculations.so right now i can view those values in a html report.but what i want to do is insert those multiple values to the database.there are 50 persons.so i want to add all the details of these 50 persons at same time by just clicking a button.how can i do this?what could be my controller view and model.
这是我在控制器中计算之后得到的值
Here are the values that i get after the calculations in controller
$sheet_data[] = array(
"employee_id" => $emp,
"name" => $allEmp->name,
"gender" => $allEmp->gender,
"br_allow" => $allEmp->allowance,
"work_days" => $working_days,
"holiday_ot_hrs" => $holiday_ot_hrs,
"holiday_ot_dys" => $holiday_ot_dys,
"holiday_ot_amount" => $holiday_ot_amount
);
推荐答案
首先你必须为你的记录创建数组。
First of all you have to create array for your records.
$sheet_data[0] = array(
"employee_id" => $emp,
"name" => $allEmp->name,
"gender" => $allEmp->gender,
"br_allow" => $allEmp->allowance,
"work_days" => $working_days,
"holiday_ot_hrs" => $holiday_ot_hrs,
"holiday_ot_dys" => $holiday_ot_dys,
"holiday_ot_amount" => $holiday_ot_amount
);
$sheet_data[1] = array(
"employee_id" => $emp,
"name" => $allEmp->name,
"gender" => $allEmp->gender,
"br_allow" => $allEmp->allowance,
"work_days" => $working_days,
"holiday_ot_hrs" => $holiday_ot_hrs,
"holiday_ot_dys" => $holiday_ot_dys,
"holiday_ot_amount" => $holiday_ot_amount
);
$sheet_data[2] = array(
"employee_id" => $emp,
"name" => $allEmp->name,
"gender" => $allEmp->gender,
"br_allow" => $allEmp->allowance,
"work_days" => $working_days,
"holiday_ot_hrs" => $holiday_ot_hrs,
"holiday_ot_dys" => $holiday_ot_dys,
"holiday_ot_amount" => $holiday_ot_amount
);
.
.
.
.
.
.
$sheet_data[50] = array(
"employee_id" => $emp,
"name" => $allEmp->name,
"gender" => $allEmp->gender,
"br_allow" => $allEmp->allowance,
"work_days" => $working_days,
"holiday_ot_hrs" => $holiday_ot_hrs,
"holiday_ot_dys" => $holiday_ot_dys,
"holiday_ot_amount" => $holiday_ot_amount
);
现在将这些数组和表名传递给insert_batch函数。
Now pass those array and table name to insert_batch function.
$this->db->insert_batch('yourtable', $sheet_data);
$this->db->insert_batch('mytable', $data);
这篇关于如何在Codeigneitor中同时插入多个记录到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!