Iteration inside while loop is not working properly

while loop example
for loop
do while loop
while loops python
multiple while loops python
while loop not working python
while loop format
while loop java

I'm trying iteration in WordPress while loop with $i. But it's printing 1 for the first item and 3 for the remaining items.

Here is an example:

<?php
    $i = 0;
    while($posts->have_posts()){
        $i++;
        $posts->the_post();

        $html .= '<div class="th-block ' . $i . '">';
        $html .= '</div>';
    }
    echo $html;
 ?>

But it's printing like this

<div class="th-block 1"></div>
<div class="th-block 3"></div>
<div class="th-block 3"></div>
<div class="th-block 3"></div>
....

Do I need to fix a limit within the loop? I searched on stackoverflow but not found any solution for this.


change name of "i" variable . wordpress some time use global variable with this name.

just change it to something like iiiii jjjjj

Typical errors with 'While' Loops, var loops = 0; is placed inside the function so that it is a local variable to the funciton 'loop' Could we not just use a while loop to repeat the logging process ? This is a free way to learn a language in a well planned and executed manner. In this tutorial, you'll learn about indefinite iteration using the Python while loop. You’ll be able to construct basic and complex while loops, interrupt loop execution with break and continue, use the else clause with a while loop, and deal with infinite loops.


It seems a very minor issue, try below code this will work for you.

<?php
    $i = 0;
    while($posts->have_posts()){
        $posts->the_post();

        $html .= '<div class="th-block ' . $i . '">';
        $html .= '</div>';

        $i++;
    }
    echo $html;
 ?>

I have not tested the code.

Loops: while and for, A single execution of the loop body is called an iteration. Any expression or variable can be a loop condition, not just comparisons: the condition is Here, the “counter” variable i is declared right in the loop. Such variables are visible only inside the loop. …it stops working: there's a syntax error. Hi! I have an issue with updating my "time" counter while working in a while loop. Essentially, the time is supposed to start at 0, and every time the while loop iterates the time is updated to time+0.01. See the pseudocode below. I would then expect the time at any point to be some multiple of 0.01.


Hello it is very simple

 <?php
        $i = 0;
        while($posts->have_posts()){
            $posts->the_post();

            $html .= '<div class="th-block ' . $i . '">';
            $html .= '</div>';

            $i++;
        }
        echo $html;
     ?>

You have to just add $i++ after your HTML content but now your HTML is displayed like this

<div class="th-block 0"></div>
<div class="th-block 1"></div>
<div class="th-block 2"></div>
<div class="th-block 3"></div>

if you want to start from one just user $i = 1 instead of zero

JavaScript while Loop By Examples, This tutorial shows you how to use the JavaScript while loop statement to For this reason, it is possible that the statement inside the while loop is never Third, in each iteration, the loop increments count by 2 and after 5 iterations, the condition count < 10 is no longer true , so the loop terminates. All Right Reserved. You only need to test that once after the loop has terminated, not on every iteration; it will only leave the loop once, so you only need to check why it left the loop once. while (in >> data) works fine for all blank streams.


Refer : php while loop with $i++

<?php
    $i = 0;
    while($posts->have_posts()){
        $posts->the_post();

        $html .= '<div class="th-block ' . <?php echo $i; ?> . '">';
        $html .= '</div>';
        $i++;
    }
    echo $html;
 ?>

3.3. While Statements — Hands-on Python Tutorial for Python 3, Other than the trick with using a return statement inside of a for loop, all of the loops In Python, while is not used quite like in English. If you make a typo and enter something that cannot be converted from a string to the right kind of number, In this exercise you iterate the jump function for specific starting values n, until� NB: while often used as "traditional" loop indices, in Matlab i and j are predefined as the imaginary sqrt(-1). You can override them as above and it is often done even in Matlab, but be aware of what you are doing when do so particularly if there's any chance whatsoever that there are real (so to speak :) ) complex values lurking


7. Iteration — How to Think Like a Computer Scientist: Learning with , But in Python, the statement a = 7 is legal and 7 = a is not. When an assignment statement is executed, the right-hand side expression (i.e. the What you will notice here is that the while loop is more work for you — the programmer — than Variables created inside a function definition are local; you can't access a local� CONTINUE statement is used in the SQL WHILE loop in order to stop the current iteration of the loop when certain conditions occur, and then it starts a new iteration from the beginning of the loop. Assume that we want to write only even numbers in a WHILE loop. In order to overcome this issue, we can use the CONTINUE statement.


Why is this dowhile loop not working?, I set the variable secret outside of the loop, then the do should run that of the loop, which is correct, so you can't re-declare it inside the loop. loop running as long as the user is getting it right (which is not what you want). Using a return inside of a loop will break it and exit the function even if the iteration is still not finished. For example: def num(): # Here there will be only one iteration # For number == 1 => 1 % 2 = 1 # So, break the loop and return the number for number in range(1, 10): if number % 2: return number >>> num() 1


Do while loop, In most computer programming languages, a do while loop is a control flow statement that As long as the continue statement is not used, the above is technically outer testing, but with a conditional EXIT LOOP somewhere inside the loop). parsers and programmers as well as avoiding the scoping problem with if . Drag a wire from the output of the error in control to the border of the while loop, and then drag the error wire from the tunnel created to the opposite border of the while loop and into the input of the Simple Error Handler.