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