【问题标题】:how to get the foreach loop value to next iteration php如何将foreach循环值获取到下一次迭代php
【发布时间】:2015-11-12 05:28:20
【问题描述】:

在我的 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 需要执行一次,插入表 3 和 4 需要表 1 和 2 的插入 ID。 即表 1 和 2 只需要 1 次插入,但 3 和 4 有多个插入 1 和 2 的 id。 如何做到这一点?

【问题讨论】:

    标签: php mysql logic


    【解决方案1】:

    您可以检查您的 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-01
      • 2023-03-03
      相关资源
      最近更新 更多