Making the title, description dynamic in header.php?

I have been working on a new website. The index.php file includes header.php, footer.php and sidebar.php.
The header.php is mostly html and I realise that the title and description will remain the same for all pages that way. How can I change that to something dynamic?

I am not using wordpress for most of it. The blog section will be hosted on there, though.

You’ll need to either use a database or array to dynamically fill the title and descriptions.

Option 1
Define a list (I’d do this if it’s a small, static number of pages you’re working with)

(rough pseudo)
if PHP servername = x.html then title = abc
else if PHP servername = y.html then title = def
else title = default

Option 2, pull from some dynamic source like your MySQL, fetch the page’s name and description from the database where you store them when you create the page.

And then spit it out, either way:

<title><?php echo $title; ?></title>
<meta type="description" content="<?php echo $description; ?>

Ahh ok. So you mean I could use something like the switch statements if the number of pages is less. Thanks.

Yes, on several of mine I just use a switch statement, works out fine, as long as you don’t need too many pages or they don’t change often.

A lot more tricky than I thought, actually. How would you go about this assuming there are about 10 more pages such as about.php, contact.php etc without using a database?

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css" type="text/css">
<title> The Index Page </title>
</head>

Include the section of code that decides how to display the title, rather than copy-pasting in each one? Would be consistent, adding a title would only mean changing the included code segment.

<title><?php include_once("title_selector.php"); ?></title>

Like for instance, what I have right now is

<?php 
 $server_name = ' **What do i set this as?**'    

switch ($server_name) {
case 'index.php':
echo ' Welcome to the home page ';
break;

case 'about.php':
echo ' Contact us here ';
break;
}

?>

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css" type="text/css">
<title> <?php echo $server_name;?> </title>
</head>

He’s using a common header for all of the pages, so I’m not sure why referencing an outside file would be necessary?

@the_loserkid

I was thinking something like this:

$page = $_SERVER['PHP_SELF']; 
switch ($page){
case: 'index.php':
 $title= 'index title here';
 $description = 'index description here';
 break;

case: 'about.php':
 $title= 'about title here';
 $description = 'about description here';
 break;
}

Thanks again, man. I tried that in xampp but it won’t show. I suppose it will show on the actual server?

after the switch statement you are outputting the results, like so:

<title><?php echo $title; ?></title>
<meta type="description" content="<?php echo $description; ?>">

right?
If all is well it should work fine in XAMPP I would think…

XAMPP Is fully capable of rendering (almost) anything a full server would. If you dont see it in your localhost, something’s not right.

I’m tend to forget to update the title and description on every page when I’m developing a website, so I do this.

<meta type="description" content="<?php echo isset($description) ? $description : 'A calendar that shows Holidays and Seasons, indivdiual days is a dynamic appointment book'; ?>">
<title><?php echo (isset($pageTitle)) ? $pageTitle : 'Calendar 2.0 beta'; ?></title>

That way if I forget to update that page, it will still have a default and I can simply give $description & $pageTitle variables the proper text on that page when I get around to it.

This switch statement thing seems more complicated than it needs to be. Why not just set a plain old regular variable before including the header?

<!-- some-page.php -->

<?php $title = 'Welcome to Some Page!' ?>
<?php $description = 'Yada yada' ?>

<?php require('header.php') ?>

And actually, I’d recommend taking that one step further. Right now, you probably include a header, then write out this page’s content, then include a sidebar, then include a footer. Instead, try turning the page’s content itself into just another variable, then combine the header, sidebar and footer into a single file called “layout” that uses the content variable.


<!-- some-page.php -->

<?php $title = 'Welcome to Some Page!' ?>
<?php $description = 'Yada yada' ?>

<?php ob_start() ?>
    <h1>Welcome!</h1>
    <p>To the super awesome Some Page</p>
<?php $content = ob_get_clean() ?>

<?php require('layout.php') ?>

<!-- layout.php -->

<!doctype html>
<html>
    <head>
        <title><?php echo htmlspecialchars($title) ?></title>
        <meta type="description" content="<?php echo htmlspecialchars($description) ?>">
    </head>
    <body>
        <div id="main">
            <?php echo $content ?>
        </div>
        <div id="sidebar">
            <h2>The super awesome sidebar</h2>
        </div>
        <div id="footer">
            <p>I'm the footer</p>
        </div>
    </body>
</html>

@Jeff_Mott That’s a bit of a broad alteration to page layout as opposed to a direct answer to the question, but I like it for some uses. I figure I don’t know enough about what OP is doing to design his layout for him though :wink:

As for setting them with a switch statement, I prefer it that way myself. I don’t want to have to make alterations to dozens of pages on a large site if I need to change a bit of wording that they share, or open each page to look back over them to make adjustments. With a ten page site? Might not be an issue either way. Whatever seems easiest to the OP is probably the thing to do. If you’re still having trouble with the switching, I’d do what @Jeff_Mott suggests.

But what if content changes from page to page like it should? Wouldn’t $content have the same value? I’m not that good with PHP just yet.

Also what you you reckon I do for the menu? I would like the active tab to be highlighted but since we’re using the same header file how can you go about this?

Each page would set its own content variable.


<!-- some-page.php -->

<?php $title = 'Welcome to Some Page!' ?>
<?php $description = 'Yada yada' ?>

<?php $activeTab = 'somePage' ?>

<?php ob_start() ?>
    <h1>Welcome!</h1>
    <p>To the super awesome Some Page</p>
<?php $content = ob_get_clean() ?>

<?php require('layout.php') ?>

<!-- some-other-page.php -->

<?php $title = 'Welcome to Some Other Page!' ?>
<?php $description = 'Yada yada 2' ?>

<?php $activeTab = 'someOtherPage' ?>

<?php ob_start() ?>
    <h1>Welcome!</h1>
    <p>To the super awesome Some Other Page</p>
<?php $content = ob_get_clean() ?>

<?php require('layout.php') ?>

Still over my head because of the functions in use but I think I understand a bit of what you mean. Include the main content with the same variables that change from page to page. Another problem I have encountered is that I have to keep the pages linked in the menu in the same place. Like for instance, my menu reads

<li><a href="index.php">Home</a></li>
<li><a href="pd.php">Popular Destinations</a>
  <ul>
    <li><a href="pd/north-pole.php">North Pole</a></li>
    <li><a href="pd/south-pole.php">South Pole</a></li>
  </ul>

So from index.php when I hit North Pole, I get to North Pole but when I hit Home from index.php, there is no index.php in the same folder as stated by the path in the header.php file.

Does that mean I have to keep everything in the main folder and not have any bifurcations? I feel like I’m doing something wrong.

A layout method like @Jeff_Mott suggested would fix that issue (of using files in differing directories) - if every page is essentially the same template page, but calling content for the Content div, then the calls will always be made from the same place and you can put the files where you want.

Regarding your earlier question, you can use $_SERVER to find the current page and compare it, if you want to add an .active class to a nav item, if you’re using a common header file for separate pages.
If you decide to use a template file, though, and just call varied Content, then you’ll probably want to use JavaScript to unset the current .active and set a new one, when you onClick a nav item?

Starting point anyway, can try and provide some examples here in a bit if you need and someone else hasn’t done.