Two conditions in a for loop - php

Why does this for loop work with each condition on their own, but together the first condition doesn't matter?
for ($j = 0; $j < 5 or $j < $synCount; $j++)
I only want the loop run five times
or
if synCount is less than this.

You probably mean "$j under 5 and $j under $sysCount", or:
$j < min(5, $sysCount)

very simple
$j < min(5, $sysCount)

You can youse the break statement to leave the for loop :
for ($j = 0; $j < 5 ; $j++)
{
if( $j >= $synCount )
break;
//treatment
}
Or calculate your limit before the loop :
$ max = $synCount < 5 ? $synCount : 5;
for ($j = 0; $j < $synCount ; $j++)
{
//treatment
}
Another solution,quickest : use min() :
for ($j = 0; $j < min(5, $synCount) ; $j++)
{
//treatment
}

Try it like this:
$loopcount = ($syncount < 5) ? $syncount : 5;
for ($j = 0; $j < $loopcount; ++$j) {
}
The first line determines whether $syncount is less than 5 or not and then assigns a value to $loopcount based on that. Then the loop runs the required number of times.

for ($j = 0; $j < ($syncCount <= 5 ? $syncCount : 5); $j++)
or slightly optimized (but I guess with 5 or less iterations this absolutely doesn't matter)
for ($j = 0, $limit = min($syncCount, 5); $j < $limit; $j++)
Anothe nice solutions
foreach (range(0, min($syncCount, 5)) as $j)
Sidenote
$syncCount <= 5 ? $syncCount : 5 == min($syncCount, 5)

$productsprice=ProductPrice::model()->findAllByAttributes(array ('product_id'=>$products_data->product_id));
foreach($productsprice AS $productsprice):
for($quantity = 0; $quantity <= 10; $quantity++)
{
echo '<li >'.array(value=>'ProductPrice::model()->getquantity($data->quantity)').'</li>' ;
}
endforeach;

Two conditions may indeed be applied inside for loop using logical operator. For example check status until it's ok
for($i = 0; $i < 100 && $status != 'ok'; $i++){
sleep(1);
$status = checkStatus();
}
Otherwise you need to use additional if expression with break
for($i = 0; $i < 100; $i++){
sleep(1);
$status = checkStatus();
if($status == 'ok') break;
}

Related

For loop (php) that results in this: 12345678910987654321

My niece is trying to create one for-loop (php), that results in this:
* 12345678910987654321
example for loop she tried:
for ($i = 1; $i <= 10; $i++ , $i = 10; $i <= 1; $i--) {
echo $i . ' ';
}
She can only use if's and elseif's. I'm not a programmer and can't really help her. Any ideas how this could be achieved in php?
Any information would be greatly appreciated.
The key is to add a variable instead of a number, then reverse that number when $i hits 10.
for($i = 1, $j = 1; $i> 0; $i+=$j) // Start i at 1, and j at 1
{
echo $i;
if($i == 10)
$j = -1; // i has hit 10, so use -1 to start subtracting
}
Another possibility is to loop up to 20, printing $i for the ascending part and 20 - $i for the descending.
for ($i = 1; $i < 20; $i++) {
if ($i <= 10) {
echo $i;
} else {
echo 20 - $i;
}
}

How to skip first two lines of a CSV file with a for loop?

I would like to read each lines of a CSV file except for the first two lines of the file.
Right now this is my code:
for ($j = 0; $j <= count($customers) - 1; $j++)
It works if I delete the first line of my CSV file, but I really need to keep it.
try this:
for ($j = 2; $j <= count($customers) - 1; $j++)
Try this:
for ($j = 2; $j <= count($customers) - 1; $j++)
or
this:
for ($j = 0; $j <= count($customers) - 1; $j++) {
if($j < 2 ) {
continue;
}
}
I guess this is what you are looking for..

How can i make the for loop parameters dynamic

for ($i=40; $i>=30; $i--) //code will display data for top x row
for ($i=1; $i<=9; $i++) //code will display data for left y column
for ($i=29; $i>=21; $i--) //code will display data for bottom x row
for ($i=30; $i>=39; $i++) //code will display data for right y column
These 4 loops all do the same thing.
In my index.php im using "include" to get the 4 loops that are in 4 different files.
How can I make the for loop dynamic?
Algoritihm:
$i = (40,1,29,30) <--will be any of those 4
$maxlow = (30,9,21,39)
$check =(>,<) <--value depends on whether $i > or < $maxlow
$icrement = (--,++) <-- if $check is > then decrease, otherwise increment
for ($i; $i($check)=$maxlow; $i($increment) <---what i am trying to do
// $step is either 1 (incrementing) or -1 (decrementing)
foreach (range($begin, $maxlow, $step) as $i) {
}
Settings for given loop:
$diff = -1;
$start = 40;
$stop = 30 + $diff;
The loop itself, always like this:
for ($i = $start; $i != $stop; $i += $diff)
$low = [40,1,29,30](rand(0,3);
$high = [30,9,21,39](rand(0,3);
$modifier = ($low > $high) ? -1 : 1;
for($i = $low; ($i * $modifier) < ($high * $modifier); $i += $modifier)
{
doStuff();
}
why not just use for with switch inside, like this:
for ($i=1; $i >=39; $i++) {
switch($i) {
case ($i>=1 && $i<=9):
break;
...
...
case ($i>40):
//do something
break;
}
}
It will be much more readable, and easier to understand/edit in future.

Refactoring 2 similar if statements

Would you write this code any differently? I don't mind tertiary operator by the way so any ideas that include it are also welcome
if ($stopYear < $startYear) {
for ($i = $startYear; $i >= $stopYear; $i--) {
$yearMultiOptions[$i] = $i;
}
} else {
for ($i = $startYear; $i <= $stopYear; $i++) {
$yearMultiOptions[$i] = $i;
}
}
$min = min($startYear, $stopYear);
$max = max($startYear, $stopYear);
for ($i = $min; $i <= $max; $i++) {
$yearMultiOptions[$i] = $i;
}
I don't know php, so min and max might have different syntax, but you get the idea.
You could do something like this:
$step = ($stopYear < $startYear) ? -1 : 1;
for ($i = $startYear; $i != $stopYear; $i += step) {
$yearMultiOptions[$i] = $i;
}

Can i do this (Loop in Loop)?

How can i do something like this:
for ($i = 1; $i <= $_SESSION['variable'] * $_SESSION['variable2']; $i++) {
$listo[$i] = $preparado1[$i].
for ($i = 1; $i <= $_SESSION['variable2']; $i++) {
$preparadoex[$i];
};
}
But doesnt work, i think this:
for ($i = 1; $i <= $_SESSION['variable'] * $_SESSION['variable2']; $i++) {
for ($i = 1; $i <= $_SESSION['variable2']; $i++) {
$listo[$i] = $preparado1[$i].$preparadoex[$i];
}
}
The script is simply, i have two classes the first have 2 numbers and the two have 3 numbers i should "connect" all numbers (2) in the first class with all the numbers of the second class:
F-S
1-1,
1-2,
1-3,
2-1,
2-2,
2-3
Thanks
You're re-defining $i inside of the second loop, which conflicts with the first loop's counter. Use another variable, like $j:
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ ){
for($j = 1; $j <=$_SESSION['variable2']; $j++){
$listo[] = $preparado1[$i] . $preparadoex[$j];
}
}
You just need to use a different variable name to increment the second 'for' loop.
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ )
{
for($j = 1; $j <=$_SESSION['variable2']; $j++)
{
$listo[] = $preparado1[$i] . $preparadoex[$j];
}
}
You can have multiple nested loop, not just two. The only thing you have to make sure is that they each use a unique loop counter.
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ ){
for($j = 1; $j <=$_SESSION['variable2']; $j++){
$listo[$i] = $preparado1[$i] . $preparadoex[$i];
}
}
In your test case your second for should use another variable like $j.

Categories