在执行PHP时加载页面 [英] Load page while PHP is executing

查看:60
本文介绍了在执行PHP时加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做什么:显示正在加载的gif或文本...至少在执行php之前和期间显示黑屏.

What Im trying to do: Display a loading gif or text... at the very least show a black screen before and during the time the php is being executed.

我尝试过的.

我已经使用flush()进行了测试,直到整个php过程完成,我什么也没得到.我也不特别喜欢这个概念,但是我会采取任何措施.

I have tested using flush () and I get nothing until the entire php process is finished. I dont particularly like this concept either but I'll take anything.

我正在考虑使用两个页面来完成此任务,尽管当前项目已接近完成,并且需要花费一些时间来整合分散的html/php代码.

I am considering using two pages to accomplish this though the current project is nearly complete and would take some time to consolidate the scattered html/php code.

当前我正在执行3-simpleXML_load_file(),1-include(),1-file_get_contents() 我有javascript函数从simpleXML_Load_file()之一绘制数据.

Currently I'm doing 3-simpleXML_load_file(), 1-include(), 1-file_get_contents() I have javascript function plotting data from one of the simpleXML_Load_file()...

我想将部分代码移动到另一个文件中,但这是一项艰巨的任务.所以我喜欢一些建议或建议.

Im up for moving parts of the code to a different file but it's a big task. So id like some advise or suggestions on how to proceed.

如果我需要详细说明,请问!

If I need to elaborate more just ask!

谢谢, JT

 <html>
 <head>
 <?php
$lat = $_POST['Lat'];
$long = $_POST['Lon'];
 $weather_hourly = simplexml_load_file('http:....lat='.$lat.'&lon='.$long.'');
 ?>
 <script type="text/javascript">

 <!--Plot function-->
 $(function() 
 {
 var d = 
 [
<?php
//Pulling in hourly data to plot temp vs time
$i=0;
$array=array();
while ($i<=100)
{
    echo '['. (strtotime($weather_hourly->data->{'time-layout'}->{'start-valid-time'}[$i])*1000) .','.$weather_hourly->data->parameters->temperature->value[$i] .'],';
    $value = $weather_hourly->data->parameters->temperature->value[$i];
    array_push($array,$value);
    $i++;
}
    foreach ($array as $key => $value) 
    { 
                    $value = (string) $value;
                    $min_sec_array[] = $value;
    }
?>

</script>
 </head>
 <body>
 <div id=graph>
 </div>
 </body

推荐答案

完成此操作的主要方法是使用AJAX和多个页面.为此,第一页不应执行任何处理,只需将加载图像放在此处即可.接下来,发出AJAX请求,请求完成后,您可以在页面上显示结果或重定向到其他页面.

The main way you can accomplish this is by using AJAX and multiple pages. To accomplish this, the first page should not do any of the processing, just put the loading image here. Next, make an AJAX request, and once the request is finished, you can show the results on the page or redirect to a different page.

示例:

文件1(还必须包括jQuery),将其与加载程序动画一起放在正文中:

File 1 (jQuery must be included also), put this in the body along with the loader animation:

<script language="javascript" type="text/javascript">
$(function(){
    var mydata = {};
    $.post('/myajaxfile.php', mydata, function(resp){
        // process response here or redirect page
    }, 'json');
});
</script>

更新:这是一个基于您的代码的更完整的示例.这尚未经过测试,需要包含jQuery库,但这应该可以为您提供一个好主意:

Update: Here is a more complete example based on your code. This has not been tested and needs to have the jQuery library included, but this should give you a good idea:

文件1:file1.html

File 1: file1.html

</head>
 <body>
 <?php
$lat = $_POST['Lat'];
$long = $_POST['Lon'];
 ?>
 <!-- Include jQuery here! Also have the loading animation here. -->
 <script type="text/javascript">
$(function(){

    $.get('/file2.php?Lat=<?php echo $lat; ?>&Lon=<?php echo $long; ?>', null, function(resp){
        // resp will have the data from file2.php
        console.log(resp);
        console.log(resp['min_sec_array']);
        console.log(resp['main']);

        // here is where you will setup the graph 
        // with the data loaded
        <!--Plot function-->
    }, 'json');

});

</script>


 <div id=graph>
 </div>
 </body
 </html>

文件2:file2.php 我不确定您是否需要$ min_sec_array,但是我有这个示例返回了该值以及您之前使用的主要数据.

File 2: file2.php I'm not sure if you needed the $min_sec_array, but I had this example return that as well as the main data you were using before.

$lat = $_POST['Lat'];
$long = $_POST['Lon'];
$weather_hourly = simplexml_load_file('http:....lat='.$lat.'&lon='.$long.'');

//Pulling in hourly data to plot temp vs time
$i=0;
$main = array();
$array=array();
while ($i<=100)
{
    $main[] = array((strtotime($weather_hourly->data->{'time-layout'}->{'start-valid-time'}[$i])*1000), $weather_hourly->data->parameters->temperature->value[$i]);
    $value = $weather_hourly->data->parameters->temperature->value[$i];
    array_push($array,$value);
    $i++;
}
foreach ($array as $key => $value) 
{
    $min_sec_array[] = (string) $value;
}


echo json_encode(array(
    'min_sec_array' =>$min_sec_array,
    'main' => $main
));

exit();

?>

这篇关于在执行PHP时加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆