...

View Full Version : Code Organization: PHP Calcs Before HTML?



d'Anconia
10-07-2012, 09:53 PM
Okay so I am having problems organizing my PHP and HTML code. I realize that to include variables and whatnot one will have to insert some PHP code into the body of the HTML but I was curious: does anyone here insert the MySQL queries and other calculations before most of their HTML code?

It's just becoming a bit of a nightmare when I'm using echo for most of my HTML and I'm wondering if I should have most of my HTML hard-coded in there and just insert the smaller parts of the PHP code (while leaving as much as possible of the PHP code at the beginning.

How do you guys organize you code within a file? I suppose I could even do some of the PHP separately and include it at the top. I typically put Javascript at the bottom.

Would love to hear some of your habits.

Dormilich
10-08-2012, 11:22 AM
my organisation is relatively simple. I use a front controller pattern, so there is a general script that does the main work. functionality is provided by the appropriate classes (which I load through an autoloader) and HTML is composed by means of a template and echoed out at the end.

tangoforce
10-08-2012, 12:29 PM
my organisation is relatively simple. I use a front controller pattern, so there is a general script that does the main work. functionality is provided by the appropriate classes (which I load through an autoloader) and HTML is composed by means of a template and echoed out at the end.

+1

Everything of mine goes through one main script - index.php. That includes $_POST submisssions, $_GET requests, everything. That script then does things that would otherwise be replicated in other scripts (thus pointless) like session checking, checking if a user is logged in, checking security permissions, checking site settings etc.

Then the script calls the appropriate module - eg a contact form, picture gallery etc, runs the php code for that, generates the html CONTENT (from smaller templates using file_get_contents() and using str_replace() to put the bits in place of tags) and then puts the finished content into the main template and finally printing it out. Again my main template is full of tags that are replaced such as <title><__title__></title> being for the title of the page.

Thats it really. A template is just that IMO, a template. It should not do anything itself such as content generation or databse lookups, it should simply just hold finished content.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum