Reset Counter in While Loop

假如想象 提交于 2020-01-03 06:29:14

问题


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


回答1:


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;
    }
}



回答2:


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

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



回答3:


You can try this

if($j == $action) i{
    //your code
    $j=0;
} else {
    $j++;
} 


来源:https://stackoverflow.com/questions/23516523/reset-counter-in-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!