php loading

Reply

Join Date: Nov 2004
Posts: 89
Reputation: MrScruff is an unknown quantity at this point 
Solved Threads: 0
MrScruff's Avatar
MrScruff MrScruff is offline Offline
Junior Poster in Training

php loading

 
0
  #1
May 16th, 2006
Hello. I have a php webpage which extracts RSS feeds and displays them. The problem is it takes a while for the page to load - I expect that its looking up the latest feed data. However I was wondering if any of you knew a way I could get the page to load up the HTML first and then display the feeds. That way users aren't just left with a blank page for 5 or 6 seconds.
The page I'm talking about is here...

http://www.blackdrop.org/dashboard/music.php

Thanks for any help.

Steve!
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,041
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: php loading

 
0
  #2
May 18th, 2006
Have you considered using a third party PHP parser? I know this isn't a solution to your problem, but I use MagpieRSS PHP parser and I absolutely love it.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: php loading

 
0
  #3
May 19th, 2006
Like the Admin said,
[qoute]
Have you considered using a third party PHP parser? I know this isn't a solution to your problem, but I use MagpieRSS PHP parser and I absolutely love it.
[/quote]

you could use a third party PHP, RSS parser.
How this solves your loading problem is that most these third party RSS or XML parsers have an inbuilt caching system, that caches RSS feeds.H
(This is required in the RSS Specs).

When retrieving RSS feeds, many sites will not allow you to request a feed more than once within an hour.

If you want to cache RSS feeds yourself, you can use something like this file caching function I found on the web, when you retrieve the rss file.

[php]
// cache a (http) fopen - with cache-timeout.
function cached_fopen($file, $file_mode, $timeout_seconds = 600, $cache_path = "/tmp"){

$debug=false;

clearstatcache();
$cache_filename=$cache_path . "/" . urlencode($file) .".cached";

if ($debug) { print "local_cache creation_time =" . @filemtime($cache_filename) . " actual time = " . time() . " timeout = " . $timeout_seconds ."<p>";}

if ( ( @file_exists($cache_filename ) and ( ( @filemtime($cache_filename) + $timeout_seconds) > ( time() ) ) ) ){
// ok, file is already cached and young enough
if ($debug) { print "using cached file ($cache_filename) <p>";}
}
else
{
if ($debug) { print "cacheing file ($file) to local ($cache_filename)<p>";}

// cache file from net to local
$f = fopen($file,"r");
$f2 = fopen($cache_filename,"w+");
while ($r=fread($f,8192) ) {
fwrite($f2,$r);
}
fclose($f2);
fclose($f);
}

// ok, point to (fresh) cached file
$handle = fopen($cache_filename, $file_mode);
return $handle;

}
[/php]

Whta the funciton does is first check if there is a local version of the file available and still "fresh", then gets it from the remote server if otherwise.
So for your RSS files, it will first check if it has a local version of your RSS file saved and within the timeout, otherwise, download the RSS file.

Hope that helps.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: php loading

 
0
  #4
May 19th, 2006
[quotes]
However I was wondering if any of you knew a way I could get the page to load up the HTML first and then display the feeds.
[/quotes]

You can't really choose which part of the page to load first with PHP.
However you can use client side methods to do this.

Theres a few, heres from simple to a bit harder:

1) Use frames:
Have an iframe which serves up just the RSS html output and embed the iframe where you want on the main page.

2) Use JavaScript's innerHTML function.
You can set up your page so that the RSS data loads right at the bottom of the page.

Something like:

[html]

<body>

<div id="rss_position">

</div>

<!--

Your page content here


-->

<div id="rss_data" style="display:none;">

<?php

// retrieve rss data and echo here

?>

</div>

</body>
</html>
[/html]

This will make the rest of the page load first. The bottom of the page will load last, and it contains the RSS data.
The rss data is loaded within the div with id "rss_data" which has a display:none style set. This prevents the div from displaying on the screen.

Then you use a bit of javascript to take the rss data/html from the hidden div at the bottom of the page, to its position at the div of id "rss_position".

[code]
<script type="text/javascript">

window.onload = function() {
var rss_loader = document.getElementById('rss_data');
var rss_display = document.getElementById('rss_position');

rss_display.innerHTML = rss_loader.innerHTML;

}

</script>

The bit of javascript is executed when the document is fully loaded, and thus your rss data is loaded.
This is after you other html is already visible.

3) Use JavaScript Remoting or XMLHTTPRequest

You can use "AJAX", to retrieve the RSS data after the page is loaded.
I'll write up a blog entry on it and post the link here if you're interested.

Hope that helps.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 32
Reputation: BlazingWolf is an unknown quantity at this point 
Solved Threads: 1
BlazingWolf BlazingWolf is offline Offline
Light Poster

Re: php loading

 
0
  #5
May 19th, 2006
Well when your syndicating from 23 blogs like your page says it just going to take some time.

Since PHP can't multithread it has to do each connection one at a time.

Like digital-ether said toward the end of her post you can use AJAX to load the RSS feeds after the page is loaded.

Although I'm like 90% sure (only digitals confidence in it is making me second guess myself) #2 won't help you because even having the PHP parse last will still prevent the page from displaying before the execution is complete.

Best way to go is AJAX.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: php loading

 
0
  #6
May 20th, 2006
Originally Posted by BlazingWolf
Although I'm like 90% sure (only digitals confidence in it is making me second guess myself) #2 won't help you because even having the PHP parse last will still prevent the page from displaying before the execution is complete..
Hey, you're absolutely right!
Normally your script would have to terminate before any output is sent to the browser via HTTP.

However, you can cause your server to flush the php output to HTTP prematurely.
This is done using the flush() function. You can read more on it here: http://www.php.net/flush

If you are also buffering the php output in the script using, ob_start(), then you will have to call ob_flush() before calling flush().

flush() also does not seem to work on Win/Apache. It does on Linux/Apache though.

Also, IE needs to receive 256 bytes via http before rendering content.

Heres how you could do it:

[php]

echo 'This is some content I want shown immediately';

// echo a hundred dummy line breaks (\r\n).
// This causes IE to start rendering received http text
for($i = 0; $i < 100; $i++) {
echo "\r\n";
}

ob_flush(); // if you have used ob_start() to buffer php
flush(); // send output buffer (seperate from ob_start() buffer) to http

// now do the rss stuff
echo retrieve_rss_feeds();

[/php]
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 32
Reputation: BlazingWolf is an unknown quantity at this point 
Solved Threads: 1
BlazingWolf BlazingWolf is offline Offline
Light Poster

Re: php loading

 
0
  #7
May 20th, 2006
Very nice.

I never knew you could do that before. That could come in handy.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 89
Reputation: MrScruff is an unknown quantity at this point 
Solved Threads: 0
MrScruff's Avatar
MrScruff MrScruff is offline Offline
Junior Poster in Training

Re: php loading

 
0
  #8
May 26th, 2006
Hey thanks for all your replies. I just use a redirect page which loads up quick and then people can start using it while they wait for the feeds to load. Thankyou!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: php loading

 
0
  #9
May 26th, 2006
Hi MrScruff,

Could you post how this work and a bit of code please. I'm really interested in this solution, sounds promising.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 89
Reputation: MrScruff is an unknown quantity at this point 
Solved Threads: 0
MrScruff's Avatar
MrScruff MrScruff is offline Offline
Junior Poster in Training

Re: php loading

 
0
  #10
May 27th, 2006
Well basically I made a html page which is exactly the same as my main php page:

http://www.blackdrop.org/dashboard/music.htm
http://www.blackdrop.org/dashboard/music.php

The user loads up the htm file first which loads up really quickly and allows them to use the lyric and download searches while the redirect page works it magic by loading the next page. The htm file has this redirect code in it:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.blackdrop.org/dashboard/music.php">

It seems to work.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC