在计数器达到0之前停止循环

时间:2014-02-05 18:18:11

标签: php while-loop

目前该公司的空缺率为3。我希望它停止在0减去。

这是我的代码:

$result4 = mysqli_query($con,"SELECT no_of_vacancy FROM job_details WHERE jobscope= 'Information Technology' AND job_title='Oak 3 Films Pte Ltd (Sales Marketing Department)';");

$result5 = mysqli_query($con, "SELECT COUNT(company) FROM student_details WHERE jobscope1 = 'myJobScope' AND company = 'myCompany';");
while ($row5 = mysqli_fetch_assoc($result5))
{
   $result6 = mysqli_query($con, "UPDATE `job_details` SET `no_of_vacancy`= `no_of_vacancy` - 2 WHERE `job_title` = 'myCompany';"));
}

一旦学生被分配到该公司,我想从公司的空缺中减去一个值。

Company | Vacancy|

ABC     | 3 

一旦有3名学生被分配,我希望它显示0。现在,每次代码运行时它都会持续到-1。

3 个答案:

答案 0 :(得分:1)

停止循环执行:

while ($row5 = mysqli_fetch_assoc($result5))
{
   $result6 = mysqli_query([...]);
   if(condition)
      break;
}

要跳过当前执行:

while ($row5 = mysqli_fetch_assoc($result5))
{
   if(condition)
       continue;
   $result6 = mysqli_query([...]);
}

答案 1 :(得分:0)

你停止while循环中断; 您可以通过退出停止所有内容;

这段代码有什么意义?

答案 2 :(得分:0)

您是否已尝试使用“break”?

例如,您可以按以下方式使用您的代码:

$result4 = mysqli_query($con,"SELECT no_of_vacancy FROM job_details WHERE jobscope= 'Information Technology' AND job_title='Oak 3 Films Pte Ltd (Sales Marketing Department)';");

$result5 = mysqli_query($con, "SELECT COUNT(company) FROM student_details WHERE jobscope1 = 'Information Technology' AND company = 'Oak 3 Films Pte Ltd (Sales Marketing Department)';");
while ($row5 = mysqli_fetch_assoc($result5))
{


$result6 = mysqli_query($con, "UPDATE `job_details` SET `no_of_vacancy`= `no_of_vacancy` - 2 WHERE `job_title` = 'Oak 3 Films Pte Ltd (Sales Marketing Department)';"));

if ($result6 == 0)
{
break;
}

我希望它可以提供帮助。