views:

118

answers:

3

My webpage is using some api's together and the total process time for the page to load is around 8 seconds. I want to show a page loading image while the page is loading. Could be like the whole page is dimmed out and an image is shown which represents the page loading and once the page loads i want to go back to my page. How can i show this functionality in a php website?

Little more info: The page is not even loading until all the visualizations in the page have completely loaded. In other words, the URL of the page is not even changing as soon as the link is clicked. As soon as the link is changing, the webpage is loaded, so any solution or reason why this is happening?

I am actually using GAPI class to get Google analytics feed and using google visualization javascript api to show the images. I am using multiple GAPI for different data parameter calls since one certain combinations wont work in one command...

a sample:

$pie->requestReportData(ga_profile_id,array('browser'),array('pageviews'),'-pageviews','',$start_date,$end_date,$start_index=1,$max_results=50); $ga->requestReportData(ga_profile_id,array('date'),array('visits','visitors'),'date','',$start_date,$end_date,$start_index=1,$max_results=50);

The values returned are stored in an array and used for google visualization api.

Each of this is stored in seperate files and i am calling them using include ();

A: 

Well, there are few issues on this path.

First of all, you output html to show loading screen, and run flush() command. Ensure you do not have any gzip compression in php or apache, as content would not be sent to the browser.

Then, you have to pray that browser would be smart enough to render it and not wait for xxx kb of data till next render.

Anyway, I would invest more time in optimization. Or do a light main page and do the rest of functionality via AJAX.

BarsMonster
doing a lit version is not possible because this page has multiple visuals and api calls, that is how they wanted it... so the best option is to show an image. could you explain in more detail of your flush idea?
Scorpion King
A: 

This is not actually php. But you can do as follows:

Add the following to the head section:

<script type="text/javascript" language="javascript">
function wait()
{ 
    if(document.getElementById)
    {
        document.getElementById('waitpage').style.visibility='hidden';
    }
    else
    {
    if(document.layers)
    {
        document.waitpage.visibility = 'hidden';
    }
    else
    {
        document.all.waitpage.style.visibility = 'hidden';
    }
    }
}
</script>

Change the <body> to <body onLoad="wait();">

and add the following in the beginning of body section:

<div id="waitpage" style="left:0px; top:0px; position:absolute; layer-background-color:white; height:100%; width:100%;"> 
<table width="100%" height="100%">
    <tr>
        <td><img src="path-to-image"/></td>
    </tr>
</table>
</div>
Aman Kumar Jain
A: 

Use jQuery...

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
$(document).ready(function()
{
    $("#loading").hide();
});
</script>

Right below body start tag put...

<img id="loading" alt="" src="ajax.gif"/>

You can create some ajax loading gifs here... http://www.ajaxload.info/

Add this CSS...

#loading{position:fixed;top:50%;left:50%;z-index:1104;}

Update

Replace with this JS code, leave the googlecode line.

<script type="text/javascript">
$(document).ready(function()
{
    $("#info").load("info.php");
    $("#linechart").load("linechart.php");
    $("#piechart").load("piechart.php");
    $("#loading").hide();
});
</script>

HTML:

<div id="#info"></div>
<div id="#linechart"></div>
<div id="#piechart"></div>

Hope it helps.

Webarto
Where do i put the JQuery stuff? I just placed your jquery code on the first line of my webpage. This webpage does not have body tag in it. The site has only one template in index.php and every page that is clicked will load in the `<body>` tag of the main index.php page. I hope i am clear.When i place this jquery code on my webpage, it is not loading at all.
Scorpion King
Webarto
Thanks webarto, it is working correctly, but the problem is that the page itself is not being called immediately. I have edited my original queesion, more info is present there.`include ('info.php');include ('linechart.php');include ('piechart.php');`These files contain the GAPI calls and Google visualization API. haivng multiple includes slows downs the process?
Scorpion King
You can load them async via jquery, that means your page loads, then onload you call these three files in three different DIVs via AJAX. You can then show loading gif, and after it is done, hide it.That's the way it should be done.
Webarto
Look up below Update, try it that way, it is simple since you are new to jQuery I suppose.
Webarto
yes it is true that i have no exp with jquery. This still is not working. The gif is showing but the google visualizations are not loading. For them to load i need to run the code present in these 3 files. I dont know if you are familiar with google visualization script or not. linechart.php has a GAPI class which gets data from google analytics and then this data is stored into an array and used in the javascript of GA and with this code `<div id="chartname"></div>` the output is shown in the webpage.
Scorpion King
Oh, I understand, I thought that they are three independent files, you could try loading them separately, lets say only info.php a other two with include(), I really have no experience with this script so I can't really help like this.
Webarto