在foreach循环中,我有以下代码:
foreach ($qty as $count => $value) {
if ($esc_returnbranch == 1) {
// insert into table1 query
$stock_return_id= $this->db->insert_id();
} else {
// insert into table2 query
$purchase_return_id=$this->db->insert_id();
}
if ($esc_returnbranch == 1) {
//insert into table3 query
} else {
//insert into table4 query
}
}
此处插入表1&2 nedd仅执行一次并且插入到表3&4需要表1&2的插入id。即表1&2只需要1个插入,但是需要3&4具有多个插入,其中1&2的id。如何做到这一点?
您可以检查id的值。
$stock_return_id = null;
$purchase_return_id = null;
foreach ($qty as $count => $value) {
if ($stock_return_id == null) {
if ($esc_returnbranch == 1) {
// insert into table1 query
$stock_return_id= $this->db->insert_id();
}
}
if ($purchase_return_id == null) {
if ($esc_returnbranch != 1) {
// insert into table2 query
$purchase_return_id=$this->db->insert_id();
}
}
if ($esc_returnbranch == 1) {
//insert into table3 query
} else {
//insert into table4 query
}
}