Having a bit of a problem with a dynamic page which is taking about 10 seconds to display. Now from reading on various articles on the web, php only displays the results once the buffer is full. However, by using flush() you are able to force php to display as it’s still loading the data.
I have a query which gets product data from a table and displays the results on a page so how can I make use of flush() in the query? Is it advisable to place the flush() at the beginning of the while statement?
In anyone has any sufggestions then I would be very grateful.
#end and get rid of current buffer
ob_end_clean();
#set the new buffer to output as it goes
ob_implicit_flush(true);
#loop from 1 -> 10
foreach(range(1, 10) as $iteration){
#output the current iteration (1 -> 10)
echo $iteration, '<br />';
#wait a second before looping again
sleep(1);
}
LOL yeah you do. So grateful for your help. OK got this included in my script but still takes about 5-6 secs to load. I do have rather a big table, about 2million rows of products. I even set ‘sleep(0)’ so there was no pause but still took the same amount of time.
Here’s my query:
<?php
ob_end_clean();
ob_implicit_flush(true);
$result = mysql_query("SELECT aw_product_id, brand_name, product_name, description, aw_deep_link, aw_image_url, search_price, merchant_deep_link, merchant_name FROM products WHERE category_id = '$category' ORDER BY product_name $limit") or die("SELECT error: " . mysql_error());
?>
Apart form splitting the data downs into seperate tables is there any way I can get the data to appear as it’s loading into the buffer?
OK, took it out and a little quicker but still taking over 4 secs to display any content. Do you think in your honest opinion that perhaps a ‘please wait’ screen whilst the data loads would be a better option. I need something to show the user that the page is working and not just blank.
Know of any good articles or guides on displaying a ‘please wait’ message while the data loads? Searched on google but all seem to use ajax or javascript and need it to be php based.
That won’t work in explorer or chrome. Sorry I can’t give a solution, but I recall recently having to keep a script running after the browser output, I think the content-length header was what fixed those browsers - that is to get the page to render before the entire script finished.
Well, gather the results provided by this particular query and save them to a local file.
Then, on subsequent requests, check if you consider this file too old to use, if so query again and save. Otherwise, just read the contents of this file as a replacement datasource.
You could automate this by running a cron job to update this file at regular intervals too, this would mean your application would always use cached data.
That won’t work in explorer or chrome. Sorry I can’t give a solution, but I recall recently having to keep a script running after the browser output, I think the content-length header was what fixed those browsers - that is to get the page to render before the entire script finished.
I don’t think I cleared anything up, not sure myself how to do this - reason being, as you mentioned - any php script that takes seconds to run doesn’t belong on the front end. I was merely pointing out that that code won’t run as you expect on different browsers.
This sounds like a good idea. So instead of pulling data live, storing it for use whenever. I will take a closer look into this. Thanks guys. Oh and btw I do all my testing in chrome first and your code worked fine for me. Just thought I would let you know.
Some browsers won’t start to render the page until X bytes have been received. You can pad with whitespace. It also depends on the markup you’re using(eg, a big <table> might not be rendered progressively).
Some webserver setups buffer your output no matter what.
Anyway, most likely, you need to work on retrieving from the database faster. Profile your code. see microtime()