Jim Peters
Jim Peters

Reputation: 5

Confused by PHP headers and footers

I'm creating my second website and want to use PHP so I can easily make changes to the header and footer of every page without having to change 40+ pages.

I understand you make a header.php page and a footer.php page then use include to put it into whatever page you're creating, however I don't want the header to include doctype or title tags or anything, i just want the 'body' part of the header if you like. the actual tags on each page need to be separate as this seems to be the most simple way of keeping page titles individual to each page, is this possible? I have looked around the web and most people suggest using some php code to change the title on each page, which appears to be a workaround instead of actually preventing the problem in the first place, any ideas?

Thanks

Upvotes: 0

Views: 283

Answers (3)

Lee
Lee

Reputation: 4323

Quite simple Daniel, you can place the included file anywhere you want.

For example:

<html>
<head>
    <title>This is your title pages</title>
    <meta>This is your individual page meta data</meta>
</head>
<body>
    <?php include 'header.php' ?>
    <h2>This is your page title</h2>
    <p>This is your page content</p>
    <?php include 'footer.php' ?>
</body>
</html>

And your header file could look like this:

<header>
    <h1>This is your Site Title</h1>
</header>

And equally your footer:

<footer>
    <p>Your copyright info</p>
</footer>

This makes the header included file not actually the head tag, which is where I think you were getting confused.

Hope that makes sense.

UPDATE: So in my example, the actual compiled output would render like this:

<html>
<head>
    <title>This is your title pages</title>
    <meta>This is your individual page meta data</meta>
</head>
<body>
    <header>
        <h1>This is your Site Title</h1>
    </header>
    <h2>This is your page title</h2>
    <p>This is your page content</p>
    <footer>
        <p>Your copyright info</p>
    </footer>
</body>
</html>

Upvotes: 1

Krista K
Krista K

Reputation: 21871

I've run the entire gamut of "making sites" and didn't get serious with header/footer until I started using WordPress framework. And that was after hand coding two CMS's and a e-commerce suite of my own. :\ I like @mariobgr's answer, it's a great first step towards something of a modular construction of a site.

What I think you're looking for is more of an answer for page/container/content. header.php are classically about the HTML headers, URL/POST/GET validation, and security. Sometimes javascript is in there, too. Normally, header.php won't have <body> or <HTML> tags, those are more . Now that you're opening this concept, I'd suggest you're ready for a CMS; like wordpress: the individual content is in a MySql database and then you've got a handful of files to handle the varied functional roles across the pages of your site.

Kind of unrelated but another example for includes: My latest informal "framework" was for a pretty significant sales report generator tool. Here is a sample hierarchy; indentation are with each include:

index.php
    db.php          //  connects to database
    inc.js.php      //  everything that's in <script> tags
    inc.container.php       //  isn't actually a page, is 1/3 page wide
        class.dates.php     //  meh, should be miraculously built in
        inc.post.php        //  POST processing to prefill form variables
        inc.controls.php    //  Form w/buttons to change report info
        inc.query.php       //  epic MySql query
        inc.report.php      //  Churns the query data and spits out tables
        inc.graph.php       //  graphs data from above via CSS backround
css/            // this is for jQuery
js/             // this is for jQuery
reports.css     //  this is for me

In reality, what started as one page / index.php became inc.container.php and then I altered index.php so that it could have multiple report "pages" on the same web page.

I'm not very OOP, so I include everything I need in index.php or container.php, even though the inc's include deeper levels. A few key variables are in global scope, but container.php has the state machine that drives the logic. It's basically the "page" even though I have formatted the CSS print media to do 3 across by two rows.

jQuery / Ajax can dynamically add and delete more containers via the controls within each container, so that's kinda nifty. This makes it easy to compare similar time periods or report across months, quarters, etc. The trick to this was incrementing a global javascript variable so the jQuery knew which div ID to add or kill.

Upvotes: -1

mariobgr
mariobgr

Reputation: 2201

This is not a workaround, it's just the simplest way to achieve it. Of course, you can put some logic and in header.php to include additional file with title and stuff for that specific page, but why make your life so much harder?

<?php

$title = 'My Title';
$stuff = 'Some random stuff';

include('header.php');

And then in header.php you have

<?php

...

<title>
  <?php if(isset($title)) echo $title; else echo 'Generic Title'; ?>
</title>

Upvotes: 2

Related Questions