PHP page title

  Ade_1 10:33 26 Aug 2008
Locked

Hi,

I am after a bit of advice. I have look all over online to try and make sense of solutions to my problems, however, I am struggling immensely too and I hoped that someone here could offer me a simple solution.

I am making a website whereby each page loads header.php therefore meaning that I can't define a title for each page.

I was wondering if you could perhaps offer me a solution to solve this problem as I cannot just entry title>My page title /title> into each page, it doesn't validate and it also messes up my structure on IE7, everything shifts to the left.

So, I wondered, is there anyway, using PHP that I can title the page without using standard HTML tags in each page?

I hope this makes sense, unfortunately I cannot give you a demo of what I mean but I think you should get a general idea of what I am talking about.

Thanks in advance,

Aidan L

  beynac 14:53 26 Aug 2008

You are presumably using "include" or "require" to load header.php.

On each page, set a variable for the page title. Put this before include/require for the header. For example:
$pagetitle = "Home page";
require("./header.php");

Then use the variable in the code to create the page title in header.php. For example:
echo "<title>$pagetitle</title>\n";

  Ade_1 22:40 26 Aug 2008

Thank you ever so much for your help. That's perfect :-D !

  User-1297335 18:34 28 Sep 2008

Hi,

I am encountering a similar problem and wonder if anyone could help me out. I have no experience with php whatsoever unfortunately and the above example seems to be the vice versa of mine.

My main.php page forms the structure of all my pages including the head section and basic layout. My .inc pages have the content and content structure.

The php for this is as follows

<?php
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$topic = isset($_GET['topic']) ? $_GET['topic'] : null;
if ($pic != null) {}
else {require_once("content/page_".$page.( $topic != null ? "_".$topic:"" ).".inc");}
?>

Is there a way for each .inc page to have its own title that is called along with the content?

Thanks

  aram 12:53 16 Oct 2008

You need to do it the other way around:

Your main.php is you template, so this will be included on every page, therefore allowing you to make changes to all the pages that use main.php as their template simply by changing that one file.

The way you are doing it there is little point in having the main.php, you may as well just build each page in plain html.

So to clarify you want your main.php to be something like this:

<head>
.
.
<?php
$head
?>
.
.
</head>

<body>
.
.
$content
.
.
</body>

then a page would be something like:

<?php
$head = "some HTML';

$content = 'some more HTML';
?>

  aram 13:01 16 Oct 2008

You need to do it the other way around:

Your main.php is you template, so this will be included on every page, therefore allowing you to make changes to all the pages that use main.php as their template simply by changing that one file.

The way you are doing it there is little point in having the main.php, you may as well just build each page in plain html.

So to clarify you want your main.php to be something like this:

<head>
.
<title> <?php echo $title ?> </title>
.
</head>

<body>
.
<?php
echo $content
?>
.
</body>

then a page.php would be something like:

<?php

$title = 'a title';
$content = 'some HTML';

include(./main.php)
?>


obvously you probably want to make it a little more flexible than that, but that should give you the idea :)

  aram 13:02 16 Oct 2008

oops must have posted that half way through by mistake! Could a moderator remove my first post please? :)

This thread is now locked and can not be replied to.

Elsewhere on IDG sites

The Evil Within 2 review-in-progress

Adobe shows still-in-development tools, including automatically colourising black-and-white photos

iPhone X news: Release date, price, new features & specs

Comment transformer un iPhone en borne Wi-Fi ?