I have this piece of code that goes through an array where each position contains a line of previously pasted text. I want the for loop to skip 2 times the number of columns input by the user, once it reaches a line that contains the word "Total". I've searched around but all I found were answers for other languages. Can anybody enlighten me on this?
Code:
1 $j = -1;
2 $step = 1;
3 for($i = 0; $i < count($statisticsinput); $i++){
4 if(strpos($statisticsinput[$i], "Total") !== false){
5 $i += 2*$_POST['columno']; //Trying to make the counter skip the 2*col iterations but seems to have no effect
6 if($step !== 2){ //The word Total appears 2 times in the pasted text, the first time
7 $step++; //should keep the script going but not the second one
8 }else{
9 break;
10 }
11 continue; //After incrementing the counter 2*col, skip over the next steps and
12 } //go to the next loop. I expected it to jump 2*col loops (usually 22)
13 if($i % $_POST['columno'] == 0){
14 $j++;
15 }
16 $employees[$j][] = $statisticsinput[$i];
17 }
Many thanks.
Related
the title may be pretty confusing. Here's a script which requires a start-end and step value to add numbers to the start value:
function addieren($start, $ende, $schritt = 1) {
if ($start < $ende) {
$erg = 0;
for ($i = $start; $i <= $ende; $i += $schritt) {
$erg += $i;
yield $erg;
}
}
}
foreach (addieren(2, 10, 2) as $erg) {
echo $erg . "<br>";
}
So the start value is 2, the end value is 10 (but it's not ending at 10 as the result shows), and it should add 2 to the $i every step.
Here's the output:
2
6
12
20
30
The first output is clear to me, since the $erg was 0 and it added the 2 because of 2 steps.
But the next output is 6 and I don't get why. In the second loop, $i is 2 and and the script says: $i += $schritt so when $i is 2 and the $schritt value is also 2, why doesn't that output 4 as the second output? Hope you get what I mean.. I guess it's a pure logic error in my head.
I am adding in feed Ads to my website. I have a foreach statement that creates a list of posts. I have created a counter that is supposed to count every four posts and insert the Ad content then repeat.
I have tried some other iterations of this but this is the one I can actually get to do something. I can find a lot of info on this exact thing pertaining to wordpress. But I am running cake php and would prefer a pure php solution.
<?php
$count = 1;
foreach($stories as $story) {
echo '<h2>'.$story->title.'</h2>';
if(!empty($story->excerpt)) {
echo $story->excerpt;
} else {
echo limit_text($story->body);
}
if ($count % 4 == 1) {
echo AD_SENSE_INFEED;
}
}
$count++;
?>
This code is what I currently have but its not working the way I would like it to. As if now it basically goes every other. So POST, AD, POST AD...etc.
Your problem isn't a coding problem, its a math problem. What you're using is called modulos or remainders basically.
So that said:
if ($count % 4 == 1) {
For it to equal 1 we have to feed in something that goes in evenly once and leaves one more.
What you want to do is:
if ($count % 4 == 0) {
Aka it means there's no remainder, 4 goes into it evenly with nothing left over.
As #RiggsFolly mentioned and I completely missed this(Give his comment a up vote) your $count variable should be incremented inside the loop as well otherwise it will only increment once after the loop ends.
You can get rid of count all together (and just use the numeric index of the array)
//just some "test" data
$stories = array_fill(0, 100, []);
foreach( $stories as $count => $story) {
echo $count." ".($count % 4)."\n";
if ($count % 4 == 3) {
echo "--------------------------------------\n";
}
}
Output:
0 0
1 1
2 2
3 3
--------------------------------------
4 0
5 1
6 2
7 3
--------------------------------------
...
Sandbox
If your not sure if the keys are in proper order, you can reset them:
foreach(array_values($stories) as $count => $story) {
Obviously an array starts at 0, so you have to offset the % result a bit ... lol ... Yes I am to lazy to increment.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a problem understanding how is the result 34 when it should be 32.Because the loop run's 4 times so when u add up 8 to the variable age it should bring up the sum as 32.Maybe I'm wrong please help out to understand.TQ
<?php
$age=24;
for($i=0; $i<=4; $i++){
$age= $age + 2;
}
echo ("At the end of the loop age = $age" );
?>
Result >>>>>>At the end of the loop age = 34
Your loop is not running four times; it's running five times.
$i<4 means the loop will terminate when $i reaches four - it terminates before that execution happens. $i<=4 means "continue looping so long as $i is less than or equal to four"
So, let's work through the examples:
for($i=0; $i<=4; $i++)
Begins, sets $i to value of 0
Loop: is $i less than or equal to 4? $i=0, so yes. $age += 2. ($age now equals 26).
End of first loop: $i++ ($i now equals 1).
Loop: is $i less than or equal to 4? $i = 1, so yes. $age += 2; ($age now equals 28).
End of second loop: $i++ ($i now equals 2).
Loop; is $i less than or equal to 4? $i = 2, so yes. $age += 2; ($age now equals 30).
End of third loop: $i++ ($i now equals 3).
Loop; is $i less than or equal to 4? $i = 3, so yes. $age += 2; ($age now equals 32).
End of fourth loop: $i++ ($i now equals 4).
Loop; is $i less than or equal to 4? $i = 4, so yes -- $i is equal to four, as specified by <=. $age += 2; ($age now equals 36).
End of fifth loop: $i++ ($i now equals 5).
Loop; is $i less than or equal to 4? $i = 5, so no. Loop terminates.
Final result: $age = 36
The number of elements between 0 and positive N is N+1.
You loop through 0 1 2 3 4.
Those are 5 iterations.
24 + (2*5) = 34.
the loop runs from i=0 to i=4
i age
0 26
1 28
2 30
3 32
4 34 ------>loop stops when i=5 since 5<=4 turns false
5
it should be
<?php
$age=24;
for($i=0; $i<4; $i++){
$age= $age + 2;
}
echo ("At the end of the loop age = $age" );
?>
the loop runs from i=0 to i=3
i age
0 26
1 28
2 30
3 32 ------>loop stops when i=4 since 4<4 turns false
4
5
Well that's because the loop starts from 0, so it runs 5 times (0,1,2,3,4).
Loop 1 (when value is 0): 24 + 2 = 26;
Loop 2 (when value is 1): 26 + 2 = 28;
Loop 3 (when value is 2): 28 + 2 = 30;
Loop 4 (when value is 3): 30 + 2 = 32;
Loop 5 (when value is 4): 32 + 2 = 34;
you can either start with 1 as:
for($i=1; $i<=4; $i++)
or make it < 4 instead of <= 4, as:
for($i=0; $i<4; $i++)
I need help. I need a loop that can execute some codes every 10 rows.
Suppose this is the scenario:
$rows = 15; // The row is for a generated report. I need to put a Thick Border, Horizontally every 10 rows.
This is the loop, and inside it I want another loop or any idea how can I execute some command every 10 rows assuming $row = 15, obviously the command should be executed once since the rows is only 15 and command will execute every 10 rows. Thanks everyone :)
$rows = 15;
for($c=0;$c<$size3;$c++)
{
//Location I want to execute a command every 10 rows.
}
Try for loop for this
for($i = 1;$i <= 40;$i++) {
if($i % 10 == 0) {
// your horizontal code here
} else {
// non horizontal code here
}
}
Edit Remember start the loop from 1 not from 0. See codepad
With 0
With 1
seems there is problem in $rows value, insted of adding condition on $rows %10 ==0 you may try setting one counter $i in loop which will get increment for each row and then you can try adding condition on $i %10 ==0
$startrow = 10 //10 if base 1 or 9 if base 0
$endrow = 15 //you said 15 so lets go with this
for($row = $startrow; $row < $endrow; $row+=10)
{
//do your thing here
}
Since you only want to perform the action once every 10 rows, why not start at row 10, then go by increments of 10 instead of incrementing the row 1 by 1.
This should be more efficient since there will be no wasted loops given your circumstance, it also eliminates the need for checking if the row is divisible by 10.
Using a php like so..
for($i = 0; $i < 30; $i++) ...
I have this html element that is rendered several times. I want to, each time we arrive at the sixth element, it adds a "style:margin-right: 0px;" for example.
My question is:
How can we find always the 6th element ?
Update: So that can mark the 6th element, then the 12th element, then the 18th element then the 24th and, at least, the 30th.
Thanks in advance,
MEM
You can use the modulo operator, %:
for ($i = 0; $i < 30; $i++) {
if ($i % 6 == 5) {
# Add what you want---I don't use PHP much
}
}
The modulo operator, %, divides the left hand side by the right hand side, and then reports the remainder of the result. So, for instance, 15 % 6 == 3, because 15 == 6*2 + 3. In the expression a % b == c, c will range from 0 to b-1. If you had $i % 6 == 0 in the above test, it would style the first element, the seventh element, etc.; this way, it'll style the sixth element, the twelfth element, etc. This is because when you're on the sixth element, $i == 5, and 5 % 6 is of course 6. For more information, check out what Wikipedia has to say about the modulo operation.
Check that the mod of $i and 6 is 0 (means that $i is evenly divisible by 6).
for($i = 0; $i < 30; $i++) {
if($i % 6 == 0) {
// this is a sixth element
}
...
}
If you don't want this to happen on the first iteration ($i == 0), you'll also need to add that check to the if statement:
if($i > 0 && $i % 6 == 0){
}
you can try using modulus (%)
if(!($i % 6)) {
// add style
}
or
if(($i % 6) == 0) {
// add style
}
EDIT: Kaleb beats me to it =/