Question

I would like to reset counter in the following loop. Have no idea how to do it

$j = 1; 
while($row = mysqli_fetch_array($check)) { 
$value = $row['tmpi'];

$calculate = mysqli_query($link,"SELECT * FROM tmpi_db WHERE  tmpi_id = '".value."'");  

$action = mysqli_num_rows($calculate); 


if($j == $action) {

//RESET $j, $j must be one again, nothing is working I have tried $j = 1;
//It's keep incrementing 1,2,3,4,5,6,7.....
}

}
$j++

Please help

Was it helpful?

Solution

Something like this should work. Simply start at zero, increment it in beginning and reset the $j if the action occurs, the next time it loops the $j will increment again and be in zero configuration (i.e. contain value 1):

$j = 0; 
while($row = mysqli_fetch_array($check))
{
    $j++;
    $value = $row['tmpi'];
    $calculate = mysqli_query($link,"SELECT * FROM tmpi_db WHERE  tmpi_id = '".value."'");
    $action = mysqli_num_rows($calculate); 
    if($j == $action)
    {
        $j = 0;
    }
}

OTHER TIPS

Just move your $j++ in an else section eg

if($j == $action){
 //stuff and reset
}else{
 $j++;
}

You can try this

if($j == $action) i{
    //your code
    $j=0;
} else {
    $j++;
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top