本文目录一览:
使用PHP如何做到每隔5分钟的时间,首页会向服务器轮询一次,以便获得最新的数据。
用js的setInterval
写一个定时器,每隔5分钟,使用ajax请求一次数据。
php隔一段时间换一组数据
class Test {
private $begin_date;
private $root;
private $user;
private $password;
private $db;
private $table;
function __construct($begin_date, $root, $user, $password, $db, $table) {
$this->begin_date = $begin_date;
$this->root = $root;
$this->user = $user;
$this->password = $password;
$this->db = $db;
$this->table = $table;
}
private function Db() {
$mysqli = new mysqli("$this->root", "$this->user", "$this->password", "$this->db");
$sql = "select * from `$this->table`";
$res = $mysqli->query($sql);
return $res->num_rows;
$res->close();
$mysqli->close();
}
private function Day_num() {
$day_num = round(abs(strtotime(date('y-m-d')) - strtotime($this->begin_date)) / 86400, 0);
return $day_num;
}
private function Date_num() {
$k = 0;
for ($i = 0; $i < round($this->Db() / 3); $i++) {
for ($j = 0; $j < 3; $j++) {
$date_arr[$i][] = $k;
$k = $k + 1;
}
}
return $date_arr;
}
private function Sql_num() {
$k = 0;
for ($i = 0; $i < round($this->Db() / 3); $i++) {
for ($j = 0; $j < 3; $j++) {
$sql_arr[$i][] = $k;
$k = $k + 1;
}
}
return $sql_arr;
}
function Result() {
$mysqli = new mysqli("$this->root", "$this->user", "$this->password", "$this->db");
$date_num = $this->Date_num();
$sql_num = $this->Sql_num();
for ($i = 0; $i < count($date_num); $i++) {
if (in_array($this->Day_num(), $date_num[$i])) {
$sql = "select * from `$this->table` limit {$sql_num[$i][0]},3";
$res = $mysqli->query($sql);
echo "<table border='1'>";
while ($row = $res->fetch_row()) {
echo "<tr>";
echo "<th>" . $row[0] . "</th>";
echo "</tr>";
}
echo "</table>";
$res->close();
$mysqli->close();
break;
}
}
}
}
$a = new Test("2014-06-27", "localhost", "root", "iuuiiuoi", "test", "test");
$a->Result();
?>
我已经测试可行,而且数据库不限制一定要有30条。无论你有多少条。就是三天为一个维度输出相同的三条。 但有一点,当读取到数据库最后几条后,再往下就不显示了。需要手动更改日期。那个注释掉的方法,本来是打算写一下的,但没时间弄了。如果有需求,后续我再改一下。
php能不能每隔一段时间插入一条数据
可以,第一种:可以在服务器上写一个定时任务,每隔一段时间调用一个插入数据接口。
第二种:foreach
里面写sleep(1);
暂停
PHP如何每隔一段时间取一段数据?
你要用什么来做?可以开一个shell来执行一个永远循环的php脚本,也可以用js触发,也可以用crontab来执行。