php抓取网页数据插入数据库(目标现有页面的数据需要刷新网页才能实现自动显示数据到前端 )
优采云 发布时间: 2022-03-02 14:15php抓取网页数据插入数据库(目标现有页面的数据需要刷新网页才能实现自动显示数据到前端
)
目标
现有页面的数据需要刷新才能获取,需要自动将数据展示到前端。
思考
因为是ThinkPHP框架,所以需要先在controller中创建一个纯数据页面,用M方法找到数据库的值放到这个页面上。这里的查询添加了一些必要的条件,跳转到不满足条件的页面。
public function mydataonlydata(){
$myhealth=M("my_health");
$userId=getSessionUserId();
$theStatus = 1;
$row=$myhealth->field("startime,status,afterHeight")->where("memberId='%s' and status=%d",$userId,$theStatus)->order("startime desc")->find();
if(!$row){
$this->redirect('Wap/User/bangding');die;
}
$afterHeight = $row['afterHeight'];
echo $afterHeight;
}
setInterval() 方法使纯数据页面每 10 秒刷新一次,refresh() 方法将数据显示到前端。
$(function () {
setInterval("refresh()",10000);
})
function refresh() {
var url="{:U('Wap/User/mydataonlydata')}";
$.post(url,function(msg){
getVal(msg);
})
}
function getVal (msg) {
var afterHeight = msg;
var ah = document.getElementById("ah");
ah.innerHTML = afterHeight;
}