Forum Moderators: coopster

Message Too Old, No Replies

Page Titles - Am I Overcomplcating This?

Am I overcomplicating my title includes?

         

Kami

1:00 am on Feb 17, 2004 (gmt 0)

10+ Year Member



Hi. I'm an average PHP user, and I've been trying to redesign my site by including pages like this:

index.php?page=somepage

Now, my problem is the way I call titles. I want to include a title from my pages (located in a directory "/pages"> - but it's not working as I hoped it would.

See, I'm trying to be clever, maybe too clever. On the index.php page, I'm including my header.htm with

<HTML><HEAD><TITLE><?=$title?></TITLE></head>
, then the main script, and then the footer.htm, but instead of putting my page title on the index page which calls the content pages, I want to include it from the pages being called. Which, as I'm sure some of you know, doesn't work (I've learned that!).

Just wondered if anyone knew a way PAST this somehow? I've spent a week trying to figure out how to get past this... it's probably pointlessly easy and I'm missing something, but I can't see it. I know I can add an extra tag onto my links to change page titles, but I just find that an extremely fussy way of doing this. I just want to include the page title in the pages I'm calling.

or am I just overcomplicating the whole thing?

CompressedAir

1:43 am on Feb 17, 2004 (gmt 0)

10+ Year Member



Hmm not exactly sure if I completely understand your problem..You have index.php and then the title comes from /pages/somepage.php. When you request index.php you want index.php's given title, but when you use somepage.php to build the page, you want that page's title to be given..If that is the case, I think this will do the trick:
/header.htm
<?
include("/pages/somepage.php");
if (!isset($title) ) $title = 'Index.php Title Here';
echo "<title>".$title."</title>";
?>

On second, thought since this depends on the page variable, something like this might be more efficient:
/header.htm

<?
include("/pages/" . $page); //load the page from the current var in the url
if (!isset($title) ) $title = 'Index.php Title Here';
echo "<title>".$title."</title>";
?>

/pages/somepage.php

<?
$title="somepage title";
?>

Hopefully that leads you in the right direction!

Kami

2:42 am on Feb 17, 2004 (gmt 0)

10+ Year Member



Interesting, but I'll try and make it clearer.

Currently, my index.php page is set up as such:


<?php
include ("theme/header.htm");

(page calling script)

include ("theme/footer.htm");
?>

Now, this WILL WORK if I call the page title as such:


<?php

$title="My Page";

include ("theme/header.htm");

(Script)

include ("theme/footer.htm");
?>

But with this method, all the pages processed by index.php will bear the same title - which isn't what I want.

What I want to happen is to include the title INTO the page being called - say,

(somepage)


<?php
$title="My Page";
?>

Content goes here.

But I'm fast realising that it's not straight-forward getting a title from a called page.

SO, basically, I got an index.php page - to which is called header.htm, a specifed page set by the url (?page="somepage"), and footer.html. I want to shift the $title from index.php to mypage...

See, I thought this would be straight-forwards, by just including the $title tag into the page being called, and thought (hoped) that it would still work. But it doesn't.

Err... and thats pretty much it... am I making a meal out of this or what? Sheesh, even I know I'm making this far too complicated...

Basically - I want to set my page title in the pages being called. There. Phew.

CompressedAir

4:35 am on Feb 17, 2004 (gmt 0)

10+ Year Member



Ah ok..makes a bit more sense now..I think the problem you may be having is that the variable $title is being grabbed from the included page too late.
For example is something similar to this happening?
/index.php?page=somepage
<?php
$title="index title";
include ("theme/header.htm");
//which has a line like
echo "<title>".$title."</title>";

//now use your script to call the specific page (somepage)

//now title is set as:
$title="page title";
//but..title has already been defined earlier

include ("theme/footer.htm");

?>


If this is the case, $title is being redefined, but it is too late in the page building process to be reflected in the html code.
The page title in the above code would be:
<title>index page</title>

Would something like this work:

<?php
$title="index title";
if ($page) {
include ("/pages/".$page);
}
//now the title has been redefined as PHP should see:
$title="page title"; //from the current file

//now we include the header
include ("theme/header.htm");
//which has a line like
echo "<title>".$title."</title>";

//now call the specific page as usual

//and include the footer
include ("theme/footer.htm");
?>

Now the page title in the above code would be:

<title>page title</title>

On a side note, are you using MySQL or some other database to store your information? It would be a bit easier to get the title for the current page if you just called the proper table/row from the db and then grabbed the title cell beforehand. Then you wouldn't have to worry about ever having the same title (unless of course you wanted to :) ). And you could setup a default page title too, if no title is specified.

Kami

4:39 pm on Feb 17, 2004 (gmt 0)

10+ Year Member



Makes sense, still a bit buggy and will probably need some work, but I guess I can live with it until I find an easier way... either that, or I'll have to grin and bear static section titles.

Thanks for your help. SQL is the next step but I wanna be sure I can work this out and understand it before charging off into something else. Thats just the way I learn.

Kami

11:11 pm on Feb 17, 2004 (gmt 0)

10+ Year Member



Just to point out, I found my own way of bypassing this problem.

Brace yourselves for the most obvious solution...

I moved my title/header/footer tags into the pages being called. Bingo. So all the index page does now, is call the pages.

See. It takes a huge clout to the head to see the obvious route (I mean that literally. Stupid shed roof caved in on me earlier...)...

It's probably not the cleanest or efficient method... but it works.

jatar_k

11:22 pm on Feb 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Kami,

I do the same but I also use a default. Pages with a specific title will look like so

<?
$title = "some fancy title";
include "header.php";

etc....

then in the header I use

if (!isset($title) ¦¦ $title == "") $title = "very generic title";

echo "<TITLE>",$title,"</TITLE>";

simplified obviously ;)

MedCenter

11:25 pm on Feb 17, 2004 (gmt 0)

10+ Year Member



switch ($_SERVER['REQUEST_URI']){

case "index.php":
$title = "Home page";
break;

case "contact.php":
$title = "Contact page";
break;

case "about.php":
$title = "About page";
break;

}

MedCenter

11:26 pm on Feb 17, 2004 (gmt 0)

10+ Year Member



im sorry, since you are appending? queries onto the end of your script, replace REQUEST_URI with SCRIPT_NAME.

Kami

12:11 am on Feb 18, 2004 (gmt 0)

10+ Year Member



Thankyou Jatar_K. I've been lurking here for a while, partly how I learned PHP - just never needed to sign up - until this drove me nuts! Heh heh.

Thanks for the heads up guys on setting a default title should a page name NOT be specified. Big help, and more info then I really needed but extremely useful to know! :)

Again, thanks for the heads up on the different ways of doing this. Much appreciated.