Jump to content

Detecting page to set page title


SyntheticShield

Recommended Posts

Im trying to figure out, for my website, how to determine that page that a visitor is on so I can set the page title.

 

I created an array (thinking that is what would be needed, not sure)

 

$pagetitle[0] = "Home";
$pagetitle[1] = "Links";

 

and so on for the different pages.  My hope was to use an echo statement to create a title with a page name

 

<?php echo "$sitename - $pagetitle"; ?>

 

Where my severe inexperience is getting me is how to code the php so that it knows what page it is on and then go through the array to get the appropriate page name.

 

I may be doing this completely wrong from the start as far as method is concerned, so Im open to any ideas.

Link to comment
Share on other sites

Im trying to figure out, for my website, how to determine that page that a visitor is on so I can set the page title.

 

I created an array (thinking that is what would be needed, not sure)

 

$pagetitle[0] = "Home";
$pagetitle[1] = "Links";

 

and so on for the different pages.  My hope was to use an echo statement to create a title with a page name

 

<?php echo "$sitename - $pagetitle"; ?>

 

Where my severe inexperience is getting me is how to code the php so that it knows what page it is on and then go through the array to get the appropriate page name.

 

I may be doing this completely wrong from the start as far as method is concerned, so Im open to any ideas.

 

the most obvious question here is, how are your pages being loaded?

Is each page its own individual page? Or are the pages being loaded into one page dynamically?

Link to comment
Share on other sites

Do you mean something like...

<?PHP
$titles = array ("Home Page", "Add Entry Page", "Edit Entry Page", View Entries");
if (isset($_GET('what_page') {
$what_page = int() $_GET('what_page');
if($what_page>3 OR $what_page<0) {
          what_page=0;
        };
}else{
$what_page = 0;
}
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title><?PHP echo $titles[$what_page]; ?></title>
</head>
<body>
<a href="index.php?what_page=0">Home</a>
<a href="index.php?what_page=1">Add Entry</a>
<a href="index.php?what_page=2">Edit Entry</a>
<a href="index.php?what_page=3">View Entries</a>

Link to comment
Share on other sites

Each page is its own individual page.  I pull in the template structure via include statements.

 

off of this comment, and no code given, why can't you simply state the title using the <title> element?

but

 

Well, you could, and that would probably be easier and more simplistic.  I was trying to think down the road a bit in that as pages are added, but I guess it would be six, one half dozen either way.  I was after, I suppose, a central location where page titles could be added and just have the php code do the rest.

Link to comment
Share on other sites

if (isset($_GET('what_page')
{
$what_page = int() $_GET('what_page');
if($what_page>3 OR $what_page<0)
{         
	what_page=0;       

}
}

 

should be all the code you need at the top after your array your else statement was allways setting your page to 0by not setting a else you let $what_page stay set if it passes the fail check

Link to comment
Share on other sites

Thank you all for the extremely quick replies.  Very helpful.  I really need to spend more time here and learn what I can.  I usually go through these phases where I need to learn something building a web page or two, then dont use it for a long time and have to start over.

 

Thank you all for your help so far.

Link to comment
Share on other sites

if (isset($_GET('what_page')
{
$what_page = int() $_GET('what_page');
if($what_page>3 OR $what_page<0)
{         
	what_page=0;       

}
}

 

should be all the code you need at the top after your array your else statement was allways setting your page to 0by not setting a else you let $what_page stay set if it passes the fail check

 

the above code is not syntactically correct,

 

if (isset($_GET['what_page'])
{
$what_page =  (int)$_GET['what_page'];
if($what_page > 3 || $what_page < 0)
{         
	$what_page=0;       

}
}

 

the code that litebearer posted is close to a solution. However, there are several ways to do what you are attempting to do, once you have the full grasp and understanding on what you are trying to accomplish, we can better assist you in this.

Link to comment
Share on other sites

Yeah, Im getting error messages when using the code posted:

 

I first got: Parse error: syntax error, unexpected T_STRING, expecting ')' on line 1.  I thought that may be due to the mission quotation mark, so I added one and then got

 

Fatal error: Can't use function return value in write context on line 2

 

What I was really after was just setting page title in one location/file.  Then having the php code check for what page it is on and then pull the title from the file.  Just to keep with the overall template I was making where I had one location to edit things across the site.  I have the template structure set up to do that, now I just wanted to use variable to add information like page titles.

Link to comment
Share on other sites

Yeah, Im getting error messages when using the code posted:

 

I first got: Parse error: syntax error, unexpected T_STRING, expecting ')' on line 1.  I thought that may be due to the mission quotation mark, so I added one and then got

 

Fatal error: Can't use function return value in write context on line 2

 

What I was really after was just setting page title in one location/file.  Then having the php code check for what page it is on and then pull the title from the file.  Just to keep with the overall template I was making where I had one location to edit things across the site.  I have the template structure set up to do that, now I just wanted to use variable to add information like page titles.

yes, if you use the code I posted you should not receive any errors, I think that we are over complicating this. When it comes down to it, it's a title of the document, which most people do not notice/care about anyway. The best approach for something like this is to use the simplest method possible, which, from the information that you gave me earlier, would be to set the titles of each page statically. Don't give yourself a headache over something that is really not very important.

Link to comment
Share on other sites

 

yes, if you use the code I posted you should not receive any errors, I think that we are over complicating this. When it comes down to it, it's a title of the document, which most people do not notice/care about anyway. The best approach for something like this is to use the simplest method possible, which, from the information that you gave me earlier, would be to set the titles of each page statically. Don't give yourself a headache over something that is really not very important.

 

I think that in the big picture of things, you are absolutely correct.  My thought was that Ive done all this other cool stuff (well cool to me cause I ultimately do it so little) why not this one more thing.  But overall, as you said, its something people just arent going to notice or really care about.

Link to comment
Share on other sites

As far as your wanting to set the page titles and having PHP do that.. Maybe including a comment block at the very top of each page, and having your template extract all the titles with fopen and a couple other tricks to grab that line with the comment for the title.. I mean its a bit complicated.. BUT it would ensure that if you add any pages, so long as you add that comment block specifying the title, it would automatically grab that..

Link to comment
Share on other sites

However, another, much easier approach would be to extract the file name, and do a str replace and upper case the first letter of every word seperated by either a dot or an underscore.. so if your page name was my_page, it would display My Page, same with my.page, much easier, however doesn't let you choose really, would be based on the filename, but its not bad i think, much simpler

Link to comment
Share on other sites

I had actually been thinking about that, whether or not you could just pull the page title from the file name.  Id love to do it for no other reason than just to learn the code, but I have to also think practically to and when it comes right down to it, I dont think anyone will ever notice that I dont have page names set or that they are static text vs. php derived.

Link to comment
Share on other sites

Personally, I like having each page have the same template.  That means the body will be the only thing to change.  OK, maybe the menu bar will be context-sensitive, but a simple switch will handle all that:

header.inc.php

<?php
function cleanInput($input){
if (is_array($input)){
	foreach ($input as $key=>$val){
		$out[$key] = mysql_real_escape_string($val);
	}
}
else{
	$out = mysql_real_escape_string($input);
}
return $out;
}
if (isset($_GET['page']) && strlen($_GET['page']) >= 0){
$page = cleanInput($_GET['page']);
switch ($page){
	case "home":
		$title = "Welcome Home!";
		brak;
	case "edit":
		$title = "Editing something.";
		break;
	case "cheeseburger":
		$title = "Mmmmm...cheeseburger....*drool*";
		break;
	default:
		$title = "I don't know what page you are on, but I bet you're having a good time!";
}
}
else{
$title = "I don't know what page you are on, but you shouldn't be here!";
}
//some HTML
echo "<title>$title</title>";
//the rest of your code
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.