M
M
MrMim2020-05-11 13:50:27
PHP
MrMim, 2020-05-11 13:50:27

How to make while stop for 1 second?

there is this code:

<?php
include 'db/db.php';
$sql = mysqli_query($link, "SELECT * FROM `users` WHERE `id` = 1");
$sqll = mysqli_fetch_array($sql);
$data = $_POST;
$cos = $sqll['money'];
$ran = mt_rand(1,2);
$timer = 10;
if ($data['sum'] <= $cos) {
    if (isset($data['put'])) {
            $cos -= $data['sum'];
            mysqli_query($link, "UPDATE `users` SET `money` = '$cos' WHERE `id` = 1");
            while ($timer = 0) {
                echo $timer;
                sleep(60);
                $timer -= 1;
            }
        }    
}


?>


you need to make while stop for 1 second and then continue, and then also stop for 1 second and continue until $timer is equal to 0

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
AlexBergal, 2020-05-12
@AlexBergal

while ($timer = 0) {
This is not a condition, but an assignment.
The code doesn't work the way you would expect.
Add missing !=

M
Maxim_Q, 2020-05-11
@Maxim_Q

well, you have it written sleep(60); this is a 60 second stop. If you need 1 sec it's 1. so change it to: sleep(1); this is the 1 second pause inside the while. or do you need something else? and what for in general to fence a kitchen garden from while when it is possible to specify simply length of a pause?

N
N0zzy, 2020-05-11
@N0zzy

You didn't write the main thing. Do you pause for regular site scripts or the console version? If not for the console vartant, then you can’t do this

N
Nikolay Panaitov, 2020-05-11
@Picknice

I don't understand why pause? If you want, let's say, to block access to the put command, then store the time of the last put in the table and, before executing, check whether how many seconds have passed since the last put.
If you need to subtract from the user's balance every second, then you need to get information from the database every second inside while, do calculations and vice versa + apparently you also need to store a counter for the user and check if it is not equal to zero, then subtract 1.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question