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 01-24-2013, 03:39 PM   PM User | #1
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
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.
rgEffects is offline   Reply With Quote
Old 01-24-2013, 05:39 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,657
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-24-2013, 06:05 PM   PM User | #3
schwarzenneger
New Coder

 
Join Date: Sep 2009
Location: Pakistan
Posts: 14
Thanks: 0
Thanked 2 Times in 2 Posts
schwarzenneger is an unknown quantity at this point
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.
__________________
My Web Development Blog

Last edited by schwarzenneger; 01-24-2013 at 06:06 PM.. Reason: grammar
schwarzenneger is offline   Reply With Quote
Users who have thanked schwarzenneger for this post:
rgEffects (01-24-2013)
Old 01-24-2013, 08:11 PM   PM User | #4
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
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.
rgEffects is offline   Reply With Quote
Reply

Bookmarks

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 05:38 PM.


Advertisement
Log in to turn off these ads.