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

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 に答える
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 に答える