Making the title, description dynamic in header.php?

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.

Yeah, I think some examples would be very helpful. Thanks!

To give you an idea of what I have, my average page looks something like this.

<?php 

$title = 'title';
$description = 'description';
$content_heading = ' heading ';
$content_paragraph = 'Lorem ipsum dolor sit amet';

/** various other variables go in here **/

?>


<?php include 'inc/header.php'?>

<?php include 'inc/content.php'?>

<?php include 'inc/sidebar.php'?>

<?php include 'inc/lowbar.php'?>
<?php include 'inc/footer.php'?>

I’m posting the above code because I believe Jeff is referring to a layout similar to this one. I think his language is too advanced for me at this stage.

With respect to your routes, you will want to use absolute paths i.e. start with a forward slash:

<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>

That will make your links independent of whatever folder created them.

This might be a bit advanced but if you want to take the time to see how everything can fit together then take a look at: http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html#a-front-controller-to-the-rescue

Thanks a lot for that tip. I will try that out soon enough! I checked out your link and it does appear to be on the advanced side. Do you know of any method by which I will be able to highlight the active tab?

I use something like this on my site, so all pages have the same header (I’m talking about a page header, that you see in the browser at the top of the web-page, not the contents on the head tag, though similar methods can apply) , only with a different image in it.

A page will have some variables defined:

$pagename = basename($_SERVER['PHP_SELF']); // This is for your Menu question
$pagetitle = "The Titile of the Page" ;
$headimage = "chicken";    // Filename of the Page Header Image
$imagealt = "A picture of a chicken"; // Alternate description of the Page Header Image

The header include will have something like this:

<div id="header">
<h1><?php echo $pagetitle ; ?></h1>
<img id="toppic" src="images/<?php echo $headimage; ?>.jpg" alt="<?php echo $imagealt; ?>"  />
</div>

For the menu include I have something like this:

<ul>
<li<?php if ( $pagename == "index.php" ) { echo ' id="current"' ; } ?>><a href="index.php">Homepage</a></li>
<li<?php if ( $pagename == "thispage.php" ) { echo ' id="current"' ; } ?>><a href="thispage.php">This</a></li>
<li<?php if ( $pagename == "thatpage.php" ) { echo ' id="current"' ; } ?>><a href="thatpage.php">That</a></li>
</ul>

Of course “current” is set in the CSS file.
May not be the most elegant solution, but it works.

Yep, gone with something similar. Thanks!