Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

PHP and HTML order

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

PHP and HTML order

Postby BartB » Wed Jul 16, 2003 4:00 am

Can one specify the load order of php and html?

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.

Is this possible?

Thanks in advance.
BartB
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Jul 16, 2003 3:52 am

Postby liquedus » Wed Jul 16, 2003 4:27 am

<html>
<head>
</head>
<body>
<p>some html here</p>
<?php ?>
</body>
</html>
liquedus
php-forum Active User
php-forum Active User
 
Posts: 266
Joined: Tue Apr 08, 2003 5:18 am
Location: Ottawa, Canada

Postby BartB » Wed Jul 16, 2003 4:32 am

I don't know if you're telling me or asking me something with your answer.

But the page is constructed in de way you typed. But PHP is loaded before html. When the php part is ready, the html part is shown.
BartB
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Jul 16, 2003 3:52 am

Postby liquedus » Wed Jul 16, 2003 5:11 am

the way php is interpreted is like this:

if tag is not opening php tag skip tags

encounter php: process script until closing tag

skip more html tags

so the way i showed in my previous post is the way it will get processed by the php interpreter
liquedus
php-forum Active User
php-forum Active User
 
Posts: 266
Joined: Tue Apr 08, 2003 5:18 am
Location: Ottawa, Canada

Re: PHP and HTML order

Postby swirlee » Wed Jul 16, 2003 5:44 am

BartB wrote:Can one specify the load order of php and html?

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:

Code: Select all
<html>
<body>
<?
   /* ... some PHP code ... */
?>
</body>
</html>


.. 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:

Code: Select all
<html>
<body>
   ... your HTML body ...
</body>
</html>
<?
   /* some PHP code.. */
?>


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.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Joan Garnet » Wed Jul 16, 2003 5:59 am

I think it doesn't matter as soon as all php code is invisible at the eyes of the client.
When you open a php page any php process is done in the server and the output is ONLY html.

PHP code is ALWAYS processed before any HTML output.
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby BartB » Wed Jul 16, 2003 6:05 am

First of all I'm a newbie concerning PHP.

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
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Jul 16, 2003 3:52 am

Postby swirlee » Wed Jul 16, 2003 6:32 am

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.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Joan Garnet » Wed Jul 16, 2003 6:50 am

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.

What query is that ?? ( if I can ask you :D )
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby BartB » Wed Jul 16, 2003 8:18 am

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

I do not have enough permissions to modify or adjust the database.

It is prety similair in comparison to the php-script designed to abstract some user- and groupdata from the seti@home site.
BartB
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Jul 16, 2003 3:52 am

Postby swirlee » Wed Jul 16, 2003 8:31 am

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.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby BartB » Wed Jul 16, 2003 8:53 am

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.
BartB
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Jul 16, 2003 3:52 am


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.