Curtis Crewe
Curtis Crewe

Reputation: 4336

limit actions PHP

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

Answers (3)

nicowernli
nicowernli

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

Patashu
Patashu

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

Schleis
Schleis

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

Related Questions