Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  192] [ 3]  / answers: 1 / hits: 31553  / 11 Years ago, fri, june 28, 2013, 12:00:00

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.



What I have tried.



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.



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.



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!



Thanks,
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

More From » php

 Answers
75

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.



Example:



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>


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:



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>


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();

?>

[#77322] Friday, June 28, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mitchellg

Total Points: 235
Total Questions: 117
Total Answers: 106

Location: Fiji
Member since Wed, Jul 14, 2021
3 Years ago
;