Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
10-07-2012, 09:53 PM #1
Code Organization: PHP Calcs Before HTML?
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.
Would love to hear some of your habits.
10-08-2012, 11:22 AM #2
- Join Date
- Jan 2010
- Behind the Wall
- Thanked 361 Times in 357 Posts
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.
The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.André Behrens, NY Times Software Developer
10-08-2012, 12:29 PM #3
- Join Date
- Feb 2011
- Your Monitor
- Thanked 538 Times in 525 Posts
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.
I can't really think of anything to write here now...