如果我使用“ while”,则具有无限循环的PHP函数,或者如果使用“ IF”而不是“ while”,则任务无法完全实现

时间:2019-05-02 10:04:57

标签: php

我创建了一个函数来分配符合某些条件的所有发票(以前没有分配用户,并且发票具有特定状态)。

问题在于,直到上述条件为假,我才能使该函数循环运行。

如果我在函数中使用while,它将创建一个无限循环。
如果我在“ while”末尾使用break或将while修改为“ if”,即使条件仍然为真,该函数也只会运行一次。

$sql_activ_users = "SELECT User FROM Users WHERE STATUS='active' AND User !='X' ORDER by U_ID ASC";
$result_activ_users = mysqli_query($conn, $sql_activ_users);
$activ_users = mysqli_num_rows($result_activ_users);

function userallocation ($conn,$FI_toAllocate,$TP_toAllocate,$MM_Srv_toAllocate,$MM_Mat_toAllocate,$CCG_toAllocate,$result_activ_users) {

$checksql="SELECT COUNT(1) - COUNT(CpProcessor) AS NUMBER 
           FROM TGSB_DB 
           where Workflow like '%Qsent | Sensitive data modification%' 
           or Workflow like '%Qdeclined%' 
           or Workflow like '%QAccepted | Sensitive data modification%' 
           or Workflow like '%QCalled back%' 
           or Workflow like '%Qin process | Sensitive data modification%' 
           or Workflow like '%Qis due | Sensitive data modification%' 
           or Workflow ='' or Workflow is null";
$checkresult = mysqli_query($conn, $checksql);
$nullcount=mysqli_fetch_array($checkresult);
$cnt=(int)$nullcount[0];

while($cnt > 0){

foreach($result_activ_users as $k => $value){
...sql query to update...
}//foreach end
}//while end
}//end of function userallocation ()

我希望该函数能一直执行到$cnt > 0,但是该函数当前在第一次运行后停止(如果我使用if($cnt > 0),或者如果我使用while($cnt > 0)则进入无限循环

1 个答案:

答案 0 :(得分:0)

您应该在while循环内的$cnt中添加一个减量运算符,以停止无限循环。

while($cnt > 0){
   foreach($result_activ_users as $k => $value){
      ...sql query to update...
   }//foreach end
  --$cnt; //pre-decrement
}//while end