Variable auto increment in while while

I have a variable that is a line with divs in html. and I'm trying to include in it a number that starts with one and automatically increases, so that each div is numbered in ascending order.

while ($row9 = mysqli_fetch_array($query, MYSQLI_ASSOC)) { $catname9 = $row9["catname"]; $statusid9 = $row9["id"]; $i = 1; 

this is my while ^^

this is my row that i repeat until it reaches the end of my named sql vv table

 $list9 .= '<div id="each9" style="margin-top:3px" onclick="moveTo(\'.main\', '.$i.');"></div> 

then i echo

 <?php echo $list9; ?> 

since i do the first 1, then the second repeat 2 and the third repeat 3

+5
source share
3 answers

Set up the while loop like this:

 $i = 1; while ($row9 = mysqli_fetch_array($query, MYSQLI_ASSOC)) { // looped logic here $i++; } 

It is important here to initialize the loop to and increase it at each iteration.

You can also increase it by other amounts if you wish. Just replace $i++ with $i += 2 ;

+4
source

Initialize the variable $i=1 as follows:

 $i = 1; while ($row9 = mysqli_fetch_array($query, MYSQLI_ASSOC)) { echo $i; // will print 1,2,3 to number of count. $i++; } 
+1
source

First, you need to declare an index variable before the while loop.
If you declare it inside, it will be a new variable every time it is iterated.
When this is done, you will need to increase the variable for each iteration, this can be done either using $i++ or $i+=1 .

If you want to start at 1 and increase, set it to 1 from the beginning and increase it by 1 at the end of the cycle.

how

 $index = 1; while(...) { // Do stuff... $index++; // increase. } 
0
source

All Articles