Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-07-2012, 08:53 PM   PM User | #1
d'Anconia
Regular Coder

 
d'Anconia's Avatar
 
Join Date: Jan 2010
Location: Tempe, AZ
Posts: 142
Thanks: 15
Thanked 5 Times in 5 Posts
d'Anconia is an unknown quantity at this point
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.

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.
__________________
Powerful ideas for all lovers of personal and political freedom:
Freedomain Radio
Free Talk Live
d'Anconia is offline   Reply With Quote
Old 10-08-2012, 10:22 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,863
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 10-08-2012, 11:29 AM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by Dormilich View Post
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Tags
html, organize code, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:32 AM.


Advertisement
Log in to turn off these ads.