1

基本上,我有一个函数可以返回数据库内的“项目”总数,这些项目的限制是40,如果返回的值小于40我希望它执行操作,从而将限制增加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
    }
}
4

3 回答 3

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']
}
于 2013-03-27T03:47:17.810 回答
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);
    }
}
于 2013-03-27T03:48:05.590 回答
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
    }
}
于 2013-03-27T03:44:48.263 回答