在While循环中重置计数器

时间:2014-05-07 11:26:47

标签: php loops while-loop

我想在以下循环中重置计数器。不知道该怎么做

$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++

请帮忙

3 个答案:

答案 0 :(得分:1)

只需将您的$j++移到其他部分,例如

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

答案 1 :(得分:1)

这样的事情应该有效。只需从零开始,在开始时递增它,并在动作发生时重置$j,下一次循环$j将再次递增并且为零配置(即包含值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 :(得分:0)

你可以试试这个

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