-1

基本上我有一个这样的foreach循环:

foreach($_POST['id'] as $key => $id) {
    $qty = $_POST['qty'][$key];
    $bin_location = $_POST['bin_location'][$key];
    $pdi_required = $_POST['pdi_required'.$key][0];
    $pdi_completed = $_POST['pdi_completed'][$key];
    $serial_no = $_POST['serial_no'][$key];
    $movement_by = $_POST['movement_by'][$key];
    $date_moving = $_POST['date_of_movement'][$key];
    $special_instructions = $_POST['special_instructions_admin'][$key];
    $status = strtolower($_POST['status'][$key]);

    $sql_update = "UPDATE products SET status='$status' WHERE job_id='$id'";

    $this->db->query($sql_update);
}

if ($status == 'rejected') {
    $sql_job_update = "UPDATE jobs SET status='rejected' WHERE job_id='$id'";
} elseif ($status =='pending' || $status =='returning') {
    $sql_job_update = "UPDATE jobs SET status='pending' WHERE job_id='$id'";
} else {
    $sql_job_update = "UPDATE jobs SET status='approved'";
}

但是,这是我最感兴趣的最后一部分(if ($status == whatever)。如何从 foreach 中获得最低的可能值?

这是因为我有两个表,jobs有一个已经完成的工作表,并且是工作products中请求的产品。如果产品因任何原因被拒绝,则整个工作都是失败的,因此该工作应该具有拒绝状态。如果它没有被拒绝并且只是待处理,则将其设置为待处理。如果所有字段都被接​​受,则将作业状态设置为接受。

我真正需要的是在 foreach 循环中的任何事件中获取某个状态是否为真,如果为真,则将状态设置为该值。

4

2 回答 2

1

使用在循环之前设置为 false 的单独变量。在循环内部,如果您获得特定状态,请将其设置为 true。

编辑:或者,由于您似乎需要考虑两个以上可能的状态值,因此最初将其设置为“已批准”,并使用比较函数来告诉您两个状态值中的哪一个较低。然后,在循环中,使用该函数将全局状态设置为自身的最小值和当前循环迭代的状态。

于 2013-08-23T11:25:07.570 回答
1

使用第二个变量跟踪作业是否失败

$jobStatus = 0;
foreach($_POST['id'] as $key => $id)
{
    $qty = $_POST['qty'][$key];
    $bin_location = $_POST['bin_location'][$key];
    $pdi_required = $_POST['pdi_required'.$key][0];
    $pdi_completed = $_POST['pdi_completed'][$key];
    $serial_no = $_POST['serial_no'][$key];
    $movement_by = $_POST['movement_by'][$key];
    $date_moving = $_POST['date_of_movement'][$key];
    $special_instructions = $_POST['special_instructions_admin'][$key];
    $status = strtolower($_POST['status'][$key]);

    //If any of the statuses are rejected then set
    //the job as failed, otherwise it will stay as FALSE
    if( $status == "rejected") {
        $jobStatus = 1;
    } else if( ($status == "pending" || $status == "returning") && $jobStatus != 1) {
        $jobStatus = 2;
    }

    $sql_update = "UPDATE products SET status='$status' WHERE job_id='$id'";

    $this->db->query($sql_update);
}
if ($jobStatus == 1) {
      $sql_job_update = "UPDATE jobs SET status='rejected' WHERE job_id='$id'";
} elseif ($jobStatus == 2){
      $sql_job_update = "UPDATE jobs SET status='pending' WHERE job_id='$id'";
} else {
      $sql_job_update = "UPDATE jobs SET status='approved'";
}
于 2013-08-23T11:26:23.777 回答