如何运行1个完整的循环圆而不是刷新页面,而不是检查if(),如果满足条件,则再次运行1个循环圈

What i want is after user click specific button on my webpage, I will check with if(isset()) than run the follow commend for loop.

This is what i have done so far. If $balance is 10 than for loop should run 10 times. For loop run instantly 10 times without refreshing the page but sleep() function help delay it. I need to refresh the page every 1 loop complete than continue the rest 9 loops. Every loop need to refresh page every time its run.

$balance = '10';
$wallet = $_SESSION['wallet'];

if(isset($_POST['click'])){
    if($balance != 0){
        for($x = 0; $x < $balance; $x++){
            $sql = $conn->query("UPDATE wallets SET balance = balance - 1 WHERE wallet = '$wallet' " );
            //some code to complete this 
            ....
            //this is where the page to refresh
            header("location: click.php?e=2");
        }
        sleep(5);
    }
    else{
        header("location: click.php?e=1");
    }
}

What i expected is .... 1 loop complete refresh page 1 time than continue another 1 loop than refresh page 1 more time than continue....

You don't need any loop its just decrement values will work.

$balance = ($_SESSION['balance'] ?? 0);
$wallet  = $_SESSION['wallet'];
if (isset($_POST['click'])) {
    if ($balance > 0) {
        // for ($x = 0; $x < $balance; $x++) {
        $sql                = $conn->query("UPDATE wallets SET balance = balance - 1 WHERE wallet = '$wallet' ");
        $_SESSION['wallet'] = intval($_SESSION['wallet']) - 1;
        $_SESSION['balance'] = $_SESSION['balance']-1;
        //some code to complete this
        //....
        //this is where the page to refresh
        header("location: click.php?e=2");
        // }
        sleep(5);
    } else {
        header("location: click.php?e=1");
    }
}