在循环中使用php中的下一个循环中的减少的变量

时间:2017-04-13 16:57:21

标签: php algorithm variables while-loop

我有一个表单,我希望它能更新tableA中的多行

$val = isset($_POST['val'])? filter_var($_POST['val'], FILTER_VALIDATE_FLOAT):'';
$id = isset($_POST['client'])? filter_var($_POST['client'], FILTER_VALIDATE_INT):'';

TableA如下所示:

CREATE TABLE `TableA` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`client` INTEGER NOT NULL,
`amount` DOUBLE(5,2) NULL,
`balance` DOUBLE(5,2) NULL DEFAULT NULL,
`status` VARCHAR(20) NOT NULL,
 PRIMARY KEY (`id`)
);
--Sample Data
-- INSERT INTO `TableA` (`id`,`client`,`amount`,`balance`,`status`) VALUES
-- ('1','1','0','150','Balance');
-- INSERT INTO `TableA` (`id`,`client`,`amount`,`balance`,`status`) VALUES
-- ('2','1','0','150','Balance');
-- INSERT INTO `TableA` (`id`,`client`,`amount`,`balance`,`status`) VALUES
-- ('3','1','0','200','Balance');

  SELECT * FROM TableA WHERE client='1':

 +----+--------+--------+---------+----------+
 | id | client | amount | balance |  status  |
 +----+--------+--------+---------+----------+
 |  1 |    1   |   0.00 |  150.00 | Not Paid |
 +----+--------+--------+---------+----------+
 |  2 |    1   |   0.00 |  150.00 | Not Paid |
 +----+--------+--------+---------+----------+
 |  3 |    1   |   0.00 |  200.00 | Not Paid |
 +----+--------+--------+---------+----------+

我的表单变量中包含$val=300和客户端id=1。现在我想更新客户端等于1的行。这是我的尝试:

//
$query = $db->prepare("SELECT * FROM TableA 
                       WHERE client=:id and status =:st");
$query->execute(array(':id' => $id,':st'=>'Not Paid'));
while($row = $query->fetch(PDO::FETCH_OBJ)){
     if($val>$row->balance){
          $newval = ($val-$row->balance);
          $bal = $row->balance;
          $update = $db->prepare("UPDATE TableA SET amount=amount+:bal, 
                                balance='0', status='Paid' WHERE client=:id");
          $update->execute(array(':bal'=>$bal, ':id' => $id));
     }else if($val==$row->balance){
          $bal = $row->balance;
          $update = $db->prepare("UPDATE TableA SET amount=amount+:bal, 
                                balance='0', status='Paid' 
                                WHERE client=:id");
          $update->execute(array(':bal'=>$bal, ':id' => $id));
     }else{
          $bal = $row->balance-$val;

          $update = $db->prepare("UPDATE TableA SET amount=amount+:val, 
                                balance=:bal, status='Balance' 
                                WHERE client=:id");
          $update->execute(array(':val'=>$val,':bal'=>$bal, ':id' => $id));
     }
}

在上面的循环之后,我期待以下输出:

 SELECT * FROM TableA WHERE client='1':

 +----+--------+--------+---------+----------+
 | id | client | amount | balance |  status  |
 +----+--------+--------+---------+----------+
 |  1 |    1   |   0.00 |  150.00 | Not Paid |
 +----+--------+--------+---------+----------+
 |  2 |    1   | 100.00 |   50.00 |  Balance |
 +----+--------+--------+---------+----------+
 |  3 |    1   | 200.00 |    0.00 |   Paid   |
 +----+--------+--------+---------+----------+    

循环不会更改$val值。因此,我没有获得我预期的输出,而是更新了所有金额的行。如何获得所需的输出?

1 个答案:

答案 0 :(得分:0)

每次使用部分更新行时,都需要减少$val。此外,当您用完所有$val时,会中断循环。

您更新的行应该在id = $row->id而不是client = $id的位置,因为后者将更新客户端的所有行,而不仅仅是循环中的当前行。

你应该在循环中调用$db->prepare,而不是$db->query

$query = $db->prepare("SELECT * FROM TableA 
                       WHERE client = :id and status = :st order by id desc");
$query->execute(array(':id' => $id,':st'=>'Not Paid'));
while($row = $query->fetch(PDO::FETCH_OBJ)){
    if($val>$row->balance){
        $val = ($val-$row->balance);
        $bal = $row->balance;
        $update = $db->prepare("UPDATE TableA SET amount=amount + :bal, 
                                balance='0', status='Paid' WHERE id=:id");
        $update->execute(array(':bal'=>$bal, ':id' => $row->id));
    }else if($val==$row->balance){
        $bal = $row->balance;
        $update = $db->prepare("UPDATE TableA SET amount=amount + :bal, 
                                balance='0', status='Paid' 
                                WHERE id=:id");
        $update->execute(array(':bal'=>$bal, ':id' => $row->id));
        break;
    }else{
        $bal = $row->balance-$val;
        $update = $db->prepare("UPDATE TableA SET amount=amount + :val, 
                                balance=:bal, status='Balance' 
                                WHERE id=:id");
        $update->execute(array(':val'=>$val,':bal'=>$bal, ':id' => $row->id));
        break;
    }
}