I have a php3 file containing html parts and php parts. What I want is that all of the html are loaded before the php starts to work.

It's hard to know quite what you mean. From the perspective of the interpreter, the script is "loaded" all at once. It is interpreted (processed, executed, whatever), however, in linear order, from top to bottom. So if your page looks like this:

.. the interpreter will pass the first two lines (because they're not inside PHP tags) to the output handler (e.g. the server and, in turn, to the browser) , then it will execute whatever is between the PHP tags and send its output, if any, to the server, and then it will pass the last two lines. It's important to remember that the browser never sees anything until after it has been handled by PHP, and PHP will always handle your script from top to bottom. That is, it'll handle your script in exactly the order it's written. If you have HTML before PHP, it'll pass the HTML before it interprets the PHP. If you have PHP before HTML, it'll interpret the PHP before it passes the HTML. And if you have PHP in the middle of two blocks of HTML, it'll pass the first block of HTML, interpret the PHP, and then pass the second block of HTML.

If, for some reason, you want to pass only HTML at the beginning of your script, and not interpret any PHP until the very end once all HTML output is sent, you must put all of the PHP at the end of the script:

But in the case above, once the HTML is sent there's no way to "go back" and have PHP add content in the middle, because it's already been sent to the server. (Well, unless you're using output buffering, but except in a very few cases, that's pretty much just faking it.)

I'd be very interested to know exactly what you're trying to do here and why, because it sounds like a) you're a little unclear on exactly how the HTTP model works, and b) there's probably a far easier way to do whatever it is you're after.

Last edited by swirlee on Wed Jul 16, 2003 5:48 am, edited 1 time in total.

The script is loading real-time data from a database on an other website. The search, interpretation and publication of this data could take between 10 - 45 seconds (depending on how big the requested group is). In other words: in the current situation a user is looking at a blank screen for 10-45 seconds.

The frame (body) of the site is all in html. I don't use frames, only tables. So I would be a lot happier if the body is loaded first before the content is being intrepreted.

BartB wrote:So I would be a lot happier if the body is loaded first before the content is being intrepreted.

The problem, Bart, is that in HTML, the content is inside the body. That is, if you want something generated by PHP to appear in the body of a page, it has to be between the page's <body></body> tags. If you want to give the user something to look at, you can always print your site's header and some information before you make your database calls. That is, if it's your database calls are holding up the show and you want to give the user something to look at before the database call, then just put that stuff before the database call in your script.

The only other way to do this, I'm afraid, is to use an iframe or something for your database content, which is a bad idea in general.

Also, it seems to me that if you have a database call that's taking 45 seconds, then something's seriously wrong. Even if it's on another server, your database query shouldn't be returning enough data to take that long to transfer -- doing so would imply that you're showing the user all of that data, which, from a usability standpoint, is way too much to be showing a user. If that's not the case, then it implies that you're doing a ton of records processing in PHP, which is universally a bad idea -- if the database can do the processing for you, which is usually the case, then let the database do it. PHP will never be able to do record processing as fast as the database itself, and there are few things that you can do to a set of records in PHP that you can't do more quickly on most database platforms.

Well, if that database query is actually so big that takes that long I also suggest using iframes.
Flash (ActionScript) also let you "preload" php-mysql queries, but not everybody (although almost everyone) has the plugin installed.

BartB wrote:At the moment the result is 1680 rows and 6 columns :roll: Furhtermore it calculates a couple of thing to publish overall stats.

If you can, do all of your calculations in MySQL (or whatever database you're using). All popular database software comes with a ton of built-in math functions, and you should be using them. This doesn't require you to change the structure of the databas, only your queries. Expecting PHP to gracefully handle 1680 rows simultaneously is a stretch, at best, but at worst -- what happens when you have 20 simultaneous connections? Or 100?

But if you're insistent on doing such monstrous calculations in PHP, I think your only option is caching. If you're calculating site or user statistics, allowing the statistics to be generated every time a user requests them is a bad idea. Instead, generate them twice daily. Or hourly. Then cache the results and show users the cache instead. This will save you a thousand headaches.

I agree. I was already considering caching. At the moment I’m reading (figuring out) the way to do so.

I could look into the math functions of the database. Maybe fun stuff to learn

Another consideration is not to publish the results in all its rows, but only show the overall stats. But I think publishing the results doesn’t slow down the process that much. Calculating the results before publishing does takes it time. So that thought would not do the trick.