Querying HTML vs. Storing in PHP file

Hey guys,

What would you say is more efficient? I have a site with around a hundred pages, but one day there will be hundreds of pages, all of which share some of the same attributes, such as the header, navigation bar, page footer, and some other small things.

Should I have the server query for the universal data (header, navbar, footer, etc.) every time a page is accessed, thus reducing redundancy? Or should I store the universal HTML in every PHP file to save the server trouble of querying every page load? I will have hundreds of visitors.

Current code for about.php page (using query technique):


<?php
$aboutcontent = '<h1>About The Site</h1>

	<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque suscipit lacus nec mauris dictum porta. Nam volutpat, odio eget aliquet hendrerit, turpiem ipsum dolor sit amet, consectetur adipiscing elit. Quisque suscipit lacus nec mauris dictum porta. Nam volutpat, odio eget aliquet hendrerit, turpis
	</p>
	
	<h2>Lorem ipsum dolor sit</h2>
	<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque suscipit lacus nec mauris dictum porta. Nam volutpat, odio eget aliquet hendrerit, turpis
	</p>
	
	<h2>Lorem Poopsum</h2>
	<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque suscipit lacus nec mauris dictum porta. Nam volutpat, odio eget aliquet hendrerit, turpisfd poospekf elrkam ekr porta cbarvi
	</p>';

eval("\\$nousercheck = \\"".$universal->get("nouser_check")."\\";");
eval("\\$mainnav = \\"".$universal->get('mainnav')."\\";");
eval("\\$fullfooter = \\"".$universal->get('fullfooter')."\\";");
eval("\\$aboutpage = \\"".$universal->get('aboutpage')."\\";");

print_page($aboutpage);
?>

So $nousercheck is a small piece of PHP code that’s used on all pages of the site, $mainnav is the navigation HTML that appears on all pages, $fullfooter is the footer copyright stuff, and $aboutpage is the whole page together with all elements pasted together (it also has html tag, head tag, body tag, and close tags.

print_page prints the HTML all out

The only thing that isn’t stored in the database is the varying information, which is stored in a variable $aboutcontent
This content may get pretty long on some pages of the site.

So what do you say guys? Thank you so much.

I do not understand why you are using eval() in your code for the pieces of your layout. Could you explain why? I do not see a need to do this.

Anyhow, you should probably query for this information in a central location, so you can just include one PHP file that handles the header, navbar, footer, etc. If you specified these calls in each and every file, think about how difficult it would be to change it, to add a second navbar or something.

If server strain becomes a problem in the future, which I doubt considering PHP is pretty robust in most circumstances, you could look into caching the output, which you could implement later.

Hello,

as ethanp said, it would be easier to store the information in a DB that create hundreds of pages.
You should also consider using a template engine like smarty and creating a single template which will display your pages.

To clarify, do not store the calls to these universal elements in a database. Where the code for these universal elements is located is irrelevant, but the calls to them should not be stored in a database. The calls themselves should be in a file that you include in your PHP pages, like such:

<?php
/**
 * Universal layout elements
 */
include 'base.php';

// Page code...
?>

So then I would have multiple includes used on each page? Because there would have to be an include near the top of the code for the header, then I will have HTML code for the content of the page itself that varies by page, then another include for the footer?
so like this?..


<?php include 'header.php';>
<body>
 <h1>blah</h1>
  <p>balfdjlks </p>

<?php include 'footer.php';>
</body>
</html>

Or should I have an include for the whole <head> element, and an include for the navbar section, and an include for the footer of the page?

Where should I keep the body tag? In an included file? Or in the php file itself?

The reason I am using eval is because that’s how MyBB works. You have to eval PHP code before you use it if you want to store it as a string.

You want to follow the principle of Don’t Repeat Yourself (DRY). Try something like this:

page.php:


<?php include 'header.php'; ?>
<h1>blah</h1>
<p>balfdjlks </p>
<?php include 'footer.php'; ?>

header.php:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    ...
</head>
<body>
<h1>Site Title</h1>

footer.php


<p>Copyright ...</p>
</body>
</html>

Hopefully this gives you a better idea of the concept. You will probably have more code in each file, but this presents the general idea. Just try to avoid repeating code whenever possible.

Thanks ethan :smiley: