Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in /customers/9/b/5/fun-tech.se/httpd.www/php_tutorial/class.page.php on line 130 OOP php5 HOWTO - Basic navigation

The php5 object oriented web design howto - Basic navigation

Dynamic title

Did you notice in the last example that the title in the head was always the same? This will become a problem in the future so let's change it so the calling page sends it to the page class when he creates it.

Filename: p06_ex01.php

<?php
require_once("class.page.php");
$page = new page("My page...");
?>

<h1>Hello</h1>
<p>What a nice little page</p>


More files and the navigation problem.

Up til this point we have only had one page, so it is time to add another one.

Filename: p06_ex02.php

<?php
require_once("class.page.php");
$page = new page("The second page...");
?>

<h1>Hello again.</h1>
<p>Just another page.</p>


This gives us a problem, how do we navigate from the first page to the second one and then back again? And without adding links on all pages?

It would be nice to have all the links in one place, and lucky us we have such a place. It is the page class. So lets add some basic navigation.

Filename: class.page.php

<?php
class page
{
    function __construct($title)
    {
        print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
        print "<html>\n";
        print "<head>\n";
        print "  <title>".$title."</title>\n";
        print "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n";
        print "</head>\n";
        print "<body>\n";
    }

    function __destruct()
    {
        print "<hr>\n";
        print "<a href=\"p06_ex01.php\">The first page</a><br>\n";
        print "<a href=\"p06_ex02.php\">The second page</a><br>\n";

        print "</body>\n";
        print "</html>\n";
    }
}
?>

Notice that the page that is calling don't need to be modified at all.