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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts

    Php above or inside html?

    My Standard practice has been to put a lot of my PHP queries before the Doctype and <head> tags on my pages. Things like <?php require_once (.... and query statements like this:
    PHP Code:
    mysql_select_db($database_civTekDB$civTekDB);
    $query_companyMenuRS "SELECT id, type, category, `class`, link, approval, description FROM content WHERE approval = 'yes' AND category='company' AND class = 'subMenu' AND type  NOT LIKE '%private%' ORDER BY link ASC";
    $companyMenuRS mysql_query($query_companyMenuRS$civTekDB) or die(mysql_error());
    $row_companyMenuRS mysql_fetch_assoc($companyMenuRS);
    $totalRows_companyMenuRS mysql_num_rows($companyMenuRS); 
    Then after all of that, and it could be a couple hundred lines of code on a typical page I'll run the html. After the closing html tag at the bottom I'll put the free result for the queries like this:
    PHP Code:
    <?php
    mysql_free_result
    ($pageRS);

    mysql_free_result($currentUserRS);

    mysql_free_result($masterLogRS);
    ?>
    On the current site I'm working on there are a bunch of elements that may or may not show up on certain pages. Things like side menus, accordions, rotating banner add scripts, even different types of drop down menu's for different areas of the websites. I handle these using standard php include statements placed in the appropriate div tags in the HTML. They typically look like this:
    PHP Code:
    <div id="sideMenu">
        <?php include('includes/sideMenu.php'); ?>
    </div>
    Here's the question: Does it hurt anything to put all of that PHP that is usually before and after the HTML inside the includes that I place inside the HTML in div tags? It seems to work fine but is it slowing down the site or potentially causing other problems?

    Thanks for your input.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You can lay out the PHP and HTML anyway you want. Ultimately its the end result that matters as well as consideration of when headers are pushed.
    Output is the last thing I do. Takes more memory, but its easier to modify than simply outputting.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Sep 2009
    Location
    Pakistan
    Posts
    14
    Thanks
    0
    Thanked 2 Times in 2 Posts
    If you are putting up too much queries at the top of the page -- and -- you have elements that may or may not show up on the page then you should consider re-arranging the code a tad bit.

    Personally, I open queries close to where I am going to use them. So if it is some kind of a widget I place the query just before the widget (or inside the widget if possible). That way it is slightly easier for me to track of stuff -- no need to scroll all the way to the top to, for example, find if $rs_newest_properties contains an is_active field. In fact, if I later find that newest_properties need to be displayed on two pages I put the queries + code in another file.

    Occasions where put queries before the <!DOCTYPE> is when I need to do some (sanity) checking (which almost always involve database queries); where failure means generating a 301/302/404/500 error.
    Last edited by schwarzenneger; 01-24-2013 at 06:06 PM. Reason: grammar

  • Users who have thanked schwarzenneger for this post:

    rgEffects (01-24-2013)

  • #4
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    Thanks for the confirmation that I'm not doing something that's going to get me in trouble. It's just so much easier to keep track of the 40 or so components of this site if they are put in includes so only the ones needed show up on pages viewed.


  •  

    Posting Permissions

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