限制动作PHP

时间:2013-03-27 03:42:02

标签: php

基本上,我有一个函数可以在数据库中返回总数量的“项目”,这些项目的限制是 40 ,如果返回的值小于 40 < / strong>我希望它执行操作,从而将限制增加 1 ,直到再次达到 40 ,之后我希望它停止,代码我目前使用如下所示

$count = $row['COUNT'];
foreach($result as $row) {
    if($count < 40) {
       //I want to execute a function, thus increasing the $count by one evertime
       //until it reaches 40, after that it must stop
    }
}

3 个答案:

答案 0 :(得分:1)

$count = $row['COUNT'];
foreach($result as $row) {
    if($count >= 40) {
       break; // exit foreach loop immediately
    }
    //put code here
    $count += 1; // or whatever you want it to be incremented by, e.g. $row['COUNT']
}

答案 1 :(得分:0)

我认为你想要一个while循环。 http://php.net/manual/en/control-structures.while.php

$count = $row['COUNT'];
foreach($result as $row) {
    while($count < 40) {
       //Execute the function that increases the count
    }
}

答案 2 :(得分:0)

试试这个:

function custom_action(&$count) {
    while($count++ < 40) {
          // do some cool stuff...
          $count++;
    }
}

$count = $row['COUNT'];
foreach($result as $row) {
    if($count < 40) {
        custom_action($count);
    }
}