PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table tabl ( id serial, sort bigserial, pos_changed_at timestamp null ); INSERT INTO tabl (sort) VALUES ( extract(epoch from now())*10000 ), ( extract(epoch from now() + interval '5 min')*10000 ), ( extract(epoch from now() + interval '10 min')*10000 ); SELECT * FROM tabl;
Copy Clear
Copy Format Clear
<?php $query = "SELECT sort FROM tabl ORDER BY sort;"; $stmt = $pdo->prepare($query); $insert = "INSERT INTO tabl (sort) VALUES (:sort)"; $sth = $pdo->prepare($insert); for ($i = 1; $i <= 20; $i++) { $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $sort = $rows[0]["sort"] + round(($rows[1]["sort"] - $rows[0]["sort"]) / 2); $sth->execute(["sort" => $sort]); } $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); //var_dump($rows); while (key($rows) !== null) { $a = current($rows)['sort']; $b = next($rows)['sort']; echo "$b - $a = " . $b - $a . PHP_EOL; }
Show:  
Copy Clear