tags:

views:

49

answers:

4

I'm developing a site that's pretty lightweight on the interface and mostly database driven (equal amounts read and write). It's written in PHP and I've found the easiest way to make each page is:

Page:

<?php include("header-nav.php"); ?>
  <table>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
    </tr>
    <tr>
      <td>Data Point 1</td>
      <td>Data Point 2</td>
    </tr>
    <tr>
      <td>Data Point 3</td>
      <td>Data Point 4</td>
    </tr>
  </table>
<?php include("footer.php"); ?>

header-nav.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Welcome</title>
    <link rel="stylesheet" type="text/css" href="assets/style.css" />
  </head>
  <?php include("db_connect.php"); ?>
  <body>
    <div id="wrapper">
      <h1>Welcome</h1>
      <ul id="nav">
        <li><a href="index.php">Home</a></li>
        <li><a href="data.php">Data</a></li>
      </ul>

footer.php:

    </div>
  </body>
  <?php mysql_close($dbc); ?>
</html>

All of these pages by themselves are not valid and may produce errors. Working together to create a whole page, they look great.

My question is: Would this be an efficient and easy to maintain solution?

It seems that just adding a couple includes (one at the top and one at the bottom) makes sense. Especially because the site is so lightweight that I don't need a web framework. It just feels uncomfortable to create pages without starting with <html><head>...etc.

+1  A: 

This is definitely an okay thing. I would highly recommend it. This way if you need to change the header or anything you can do so in once place easily. And the read time for hitting the file system for the include really isn't that big of a concern. So I would say that this is definitely acceptable.

Boushley
One thing to watch out for: make sure to comment which tag closes what Element or there's a chance you could get lost between files. A </div><!-- closes div#body --> will help you out a ton in that footer; otherwise, if your includes get too complicated there's a chance you could say "what the heck does this tag do?," delete it, and hose your layout.
ajm
A: 

Yes, IMO this is a perfectly good way to do things, especially for a small site. Done it myself many times.

Eric Petroelje
+1  A: 

"All of these pages by themselves are not valid" - I'm not sure what you mean by this. You mean a HTML Validator wouldn't pass them? Well of course not - they are fragments of pages. What matters is what the validator says when ran against the HTML the executed PHP generates.

This is one approach, and depending on the size of the problem you're tackling it's a valid one.

James
A: 

Get over your discomfort. Most IDEs (Dreamweaver comes to mind) actually support this way of developing sites, and will display content correctly and honor the includes if you prefer a WYSIWYG.

I develop sites using includes, like so:

site-header.inc:

require_once 'html-header.inc';
<div id="header">
/* menus, navigation, etc. */
</div>
<div class="content">

site-footer.inc:

</div>
<div id="header">
/* menus, navigation, etc. */
</div>
require_once 'html-footer.inc';

Where "html-header.inc" and "html-footer.inc" are your HTML header and footer tags and elements (title, meta, etc.). I then have functions to allow me to add CSS, JavaScript, titles, anywhere on the page, and use ob_start() and ob_end_flush() to handle these in the footer, actually. e.g.

stylesheet_register($path, $media="screen", $type="text/css");
javascript_register($path, $type="text/javascript");
title_set($title, $overwrite=true);

It your basic concept of abstraction: Don't write the same "header" and "footer" HTML code twice. Same applies to any PHP functionality which can be easily abstracted away and decoupled. Best of luck.

razzed
Be careful with using the .inc file extension. Unless your server is set to parse files with that extension, anyone can open them up and read the server-side code.
T Pops
Agreed. I actually usually structure my sites such that the web-exposed content (.php) is in a subdirectory, and headers, templates, and object code is not within the web root (or below). Good tip, though.
razzed