Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder d'Anconia's Avatar
    Join Date
    Jan 2010
    Location
    Tempe, AZ
    Posts
    149
    Thanks
    16
    Thanked 6 Times in 6 Posts

    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.
    Datagonia Web (My Portfolio)

    Powerful ideas for all lovers of personal and political freedom:
    Freedomain Radio
    Free Talk Live

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,218
    Thanks
    12
    Thanked 338 Times in 334 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

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,283
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •