I currently have a very simple page, with some more complicated backend. I\'ve always done this the same way, but I feel it\'s not right. But I haven\'t come up with much us
Methinks that you are looking for templates.
Though you almost nailed it, you are mixing matters a little. Just separate your "design parts" from "code parts". Don't make header to do the job of the actual code and everything become as clear as a fresh water
Let me suggest you the following setup:
A site contains of
the main idea is to make PHP script do no output until all data got ready - thus you will get an instant solution of your page title problem. Moreover (think about it):
Once php script done with data preparations, it calls for the the main site timplate, which, in turn, will include the actual page template.
Thus you will get exactly what you're asking for - the website dynamic enough that it can have easily editable pages, but also be easily editable is a big part of it needs editing - and many more other benefits.
Template I am talking about is a typical PHP script, however, consists mostly of pure HTML, with some PHP code only to display dynamically generated data.
Here you go with some basic yet working example
the page:
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
// setting title for using in the main template
$pagetitle = "Links to friend sites";
//etc
//set page template filename
$tpl = "links.tpl.php";
//and then finally call a template:
include "main.tpl.php";
?>
where main.tpl.php
is your main site template, including common parts, like header, footer, menu etc:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
and links.tpl.php
is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
<? endforeach ?>
<ul>
The general accepted approach in small sites is to have something like this:
<?php $title = 'Title'; ?>
<?php require_once('header.php'); ?>
<title><?php echo $title; ?> - MyWebsite</title>
I think that should work fine for you, while still preserving the K.I.S.S principle.
What I have done is make header and footer functions that take an array as a parameter, and the array may contain optional elements, so you would pass in the title, etc. to the header function.
I think one thing you will find that is "generally accepted" is to use PHP more and more as a program, and less to drop out of html when you need dynamic content only. In other words, writing everything as a program that outputs html, not as a mix of html and php (opening and closing the PHP tags).
In this model, many websites only have one "index.php" and that actually generates all the pages (often from a database). But if you've only got handful of pages, then each being it's own php page with shared header and footer functions is a fine way to go, IMHO.
I think most experienced coders would tell you to mix your code and your HTML as little as possible. This means doing all of your calculations, then simply passing any variables needed by the view to the view.
They would also tell you to look into the MVC design pattern. In PHP, this is where Controller classes take the initial request, perform the correct operations on the correct business Model classes & then pass needed data to a View class, which would render your HTML.
MVC can go from very simple 3 class variations, to extremely complex "Frameworks" like Zend Framework. If you build your own, look into PHP class autoloading, as it will make your life easier.