Page 1 of 2

Page Titles in PHP

Posted: Sat Apr 10, 2004 2:37 pm
by Aleister
I am very new to PHP, so I am trying to get some basic things down I want to do. I have my main file, index.php, which loads the content from main.php (unless another file is passed to it).

index.php :

Code: Select all

<title><?php echo $page_title ?></title>
....
<?php 	
$page_location = $x . ".php";
if($x && file_exists($page_location)) {
   	include($page_location);

}
else {
    include("main.php"); /* load if nothing else is passed */
}
?>
then in main.php :

Code: Select all

<?php $page_title = "This is the index"; ?>


I just can't get it working.. when I view the output in my browser, it just shows <title></title> with nothing in it. I know it is loading up main.php properly, because there is some content in there, and it is showing up on the page just fine. Any ideas? Thanks!

Edit: Looking back, I did not make my problem as clear as I could of. What I am trying to do is take the page_title variable from main.php and make it the title of index.php.. so each file I am using will provide the title for the page.

Posted: Sat Apr 10, 2004 3:28 pm
by qads
thats beacuse you are trying to echo the page title before the page is included, try this...

Code: Select all

<?php
$page_location = $x . ".php"; 
if($x && file_exists($page_location)) { 
      include($page_location); 

} 
else { 
    include("main.php"); /* load if nothing else is passed */ 
} 
?>
<title>
<?php echo $page_title ?>
</title>

Posted: Sat Apr 10, 2004 3:45 pm
by Aleister
Ah.. makes sense... But can the title be declared anywhere? I thought it had to be in the header... or is it a php specific thing?

Should I take it out of the header completely? Or leave a blank one there.. I don't know how the inheritance of it works if it is declared in the body.

Thanks a lot for the response!

Posted: Sat Apr 10, 2004 4:43 pm
by Ixplodestuff8
Php is completely server side, if you where to "view source" on the page displayed from the browser you wouldn't see any php code at all, just
<title>
(whatever the title is)
</title>

so it doesn't affect the html at all.

Posted: Sat Apr 10, 2004 5:25 pm
by d3ad1ysp0rk
I think he means his file is like:
<body>
blah blah, i am a file!
</body>
so if he echos the title afterwards, it will be incorrect html (body tags BEFORE head tags? bad).

if you make sure to name the files the same as you want the title, you can go:

Code: Select all

<?php 
$page_location = $x . ".php"; 
if($x && file_exists($page_location)) { 
      echo "<html><head><title>" .$x."</title></head>";
      include($page_location); 
      echo "</html>";
} 
else { 
    echo "<html><head><title>Home Page</title></head>";
    include("main.php"); /* load if nothing else is passed */ 
    echo "</html>";
} 
?>

Posted: Sat Apr 10, 2004 6:19 pm
by Aleister
Ok, the code I posted that goes in my index.php, that loads main.php.. that is just a small part of whats in index.php.. index.php has my layout.. it allready has a header section.. and the code is just in the body.. here is the full file :

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title><?php echo $page_title ?></title> 

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<p>Links:</p>
<a href="index.php">Index</a><br>
<a href="index.php?x=test1">Test Page 1</a><br>

<p>Content:</p>

<?php 	
$page_location = $x . ".php";
if($x && file_exists($page_location)) {
   	include($page_location);

}
else {
    include("main.php");
}
?>

</body>
</html>
So as you can see, I can't echo the header into the file.. it would work fine if that code was the only thing I had in there.. but theres allready stuff in there.. know what I mean?

Posted: Sat Apr 10, 2004 6:24 pm
by markl999
I would reverse the logic, ie instead of having some herader that includes the main page content, i would have main.php include header.php and footer.php, that way in main.php you can define certain vars that appear in header.php nad footer.php, eg

Code: Select all

//this is main.php
$title = 'This is the main page';
$whatever = 'some value';
require_once 'header.php'; //this can use $title now
require_once 'template/main.php';
require_once 'footer.php';
template/main.php is the html that's goes with main.php if that makes sense. This way you have a basic template system where you can separete out the html from the code. And as header.php and footer.php come 'after' main.php then you can define any variables you want to define that header or footer needs. If that makes sense :o

Posted: Sat Apr 10, 2004 6:30 pm
by Aleister
I am getting sort of confused now.. My original intention was to have index.php be the 'primary' file.. and all the content is stuck into seperate files... where main.php would be the content for the main index (when nothing else has been selected - other pages etc..).

I am trying to keep from having to have anything else in main.php, links.php, about.php (etc..) aside from the content. So when I edit the pages layout, I only edit index.php.

If that code you pasted is all thats in main.php, where does the content go ? Sorry for these questions.. as I said I am very new, and trying to get the basics all sorted.. that seems to be the hardest part to 'get into' with this.. I have not even been able to find any pages with a very simple example of what I am trying to do :(

Edit: I know it's a lot to ask, but would you want to show a full example of what would be in both index.php and main.php so I can see how it works together? If you don't feel like it i understand :)

Posted: Sat Apr 10, 2004 6:51 pm
by markl999
Ok, well here's a basic example. Lets suppose we take a site that has a common header and footer across all pages, and only the main content changes. The header and footer can still contain variables that might change per page, for example the page title in the <title></title> html tags. So lets take one example of such a page, called foo.php (could be main.php, aboutus.php, contactus.php etc..etc). We also want to separate out foo.php's php code from it's html so it's easier to update at a later stage.

So header.php might look like:

Code: Select all

<?php
//this is header.php
?>
<html>
<head>
<title><?php echo empty($title) ? 'a default title' : $title ?></title>
</head>
<body>
<div class="header">
  PUT THE HEADER HTML HERE. BASICALLY ANYTHING BEFORE THE MAIN CONTENT
</div>
<div class="maincontent">
and footer.php might look like

Code: Select all

<?php
//this is footer.php
?>
</div> <!-- closes the main content div in the header -->
<div class="footer">
  PUT THE FOOTER HTML HERE. BASICALLY ANYTHING AFTER THE MAIN CONTENT
</div>
</body>
</html>
and foo.php could look like:

Code: Select all

<?php
//this is foo.php

$title = 'This is the main page';
$welcomemessage = 'Welcome to my site';

require_once 'header.php';
require_once 'theme/foo.html.php';
require_once 'footer.php';
?>
Here we've moved foo.php's html code into another file, this has several advantages which i'll mention in a moment ;)
So theme/foo.php might look like (which holds foo.php's html)

Code: Select all

<?php
//this is foo.html.php
?>
<b><?php echo $welcomemessage ?></b>
whatever you want in foo.php can go here.
foo.php is basically the main page content, surrounded by
the header and footer
All this might look too complicated and nothing to do with what you are after, but it's the basis of a template system. With this each content page (main.php, foo.php, aboutus.php, contactus.php etc) has full control over the header and footer, so it can alter the page title etc..

Also, say you want to have multiple site themes (look and feels) then all you need to do is create some new html templates and you're done, you don't need to alter the code at all (well a little in my examples as i hard coded the theme directory). So back to your original question ... using this method then foo.php (or main.php in your code) can set $title before header.php is called, with your current code this isn't possible as the <title></title> tags come before main.php even has chance to alter it.

I apologise if this is too confusing (and i can see how it could be) but this is sort of a long term solution to the problem you have now, and it offers great flexibility). Please, please just sayb if it's too much and just ignore it, i won't be offended ;) But the basic answer to your question is the same, you need to have main.php come before the page that has the <title></title> tags in it.

Posted: Sat Apr 10, 2004 7:40 pm
by Aleister
It actually is a great answer to my question.. I am going to try out the new code now.. Theres one more little question about its implementation I might have so I will let you know how it goes.. shouldnt take too long. thanks again!!

Posted: Sat Apr 10, 2004 8:00 pm
by Aleister
so for each new page of content.. i just need a new .php file in the main directory? or a new one .html.php in the theme directory ? I thought I had that part but it slipped my mind :) I understand what you have said so far.. I just need to implement the part about the links.. so I can do something like this :

Code: Select all

<a href="index.php?x=contactus">contact us!"</a>

etc..

Posted: Sat Apr 10, 2004 8:04 pm
by markl999
Well, in your case index.php handles all the content, like x=contactus, x=aboutus, x=whatever etc..etc.. i prefer to have separate pages for each 'part' contactus.php, aboutus.php, whatever.php

Each .php file has an associated /html.php file, for example, /themes/default/contactus.html.php, or /themes/blue/contactus.php etc.. this way you can have multiple themes or at the least separate the html from the php code. So your links would look like <a href="/contactus.php">contact us!</a>.

You can still do it with your index.php?x=contactus way and the theory is still the same, it just involves an extra step (including contactus.php) so if you decide to go that way then let us know and i'll explain the difference between that and a straight <a href="/contactus.php"> type way ;)

Posted: Sat Apr 10, 2004 8:11 pm
by Aleister
I thought if I included all the parts this way, I wouldn't have to have a seperate layout file for each of the content files. so contact.php would simply be the text that needs to be put in.

Edit: Please explain the difference if you would... Your way is probably better :)

Posted: Sat Apr 10, 2004 8:24 pm
by Aleister
Ok.. I got that working.. example at http://www.templeofdagon.com/ptest/

I see what you mean about having the files seperate. I don't suppose the files in the theme dir have to have the .html.php extension? I can't see a specific reason really other than just for reference.

I suppose that if I did have different themes, I could have some sort of 'IF' statement in my main php files? so it determines there which dir under 'theme' to use?

Maybe I am getting side-tracked.. I am just trying to work out all the advantages of doing it this way.. Having to have 2 seperate files for every page seems to defeat the purpose :P

Posted: Sat Apr 10, 2004 8:32 pm
by Aleister
Sorry one more reply.. I know you mentioned it being better to seperate the content from the code.. I just don't see the advantage.. It seems I could do it like this :

index.php :

Code: Select all

<?php 

$title = 'This is the main page'; 

require_once 'header.php'; 
require_once 'theme/main.html.php'; 
?>

<p>this is the main index.php</p>

<?php
require_once 'footer.php'; 
?>
This would still let me use the theme file.. but I would only need 1.. that all the files linked to.. (assuming all the files use the same layout).. I wouldnt have to have one per file