Reputation: 4336
Basically, I have a function that returns a total amount of "items" inside of a database, The limit on these items is 40, If the returned value is less than 40 i want it to perform actions thus increasing the the limit by 1 until it reaches 40 again, after that i want it to stop, The code i'm currently using is shown below
$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
}
}
Upvotes: 1
Views: 59
Reputation: 3348
Try this:
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);
}
}
Upvotes: 0
Reputation: 21773
$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']
}
Upvotes: 1
Reputation: 43730
I think that you want a while
loop. 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
}
}
Upvotes: 0