SyntheticShield Posted January 14, 2012 Share Posted January 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/ Share on other sites More sharing options...
AyKay47 Posted January 14, 2012 Share Posted January 14, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307674 Share on other sites More sharing options...
SyntheticShield Posted January 14, 2012 Author Share Posted January 14, 2012 Each page is its own individual page. I pull in the template structure via include statements. Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307675 Share on other sites More sharing options...
blacknight Posted January 14, 2012 Share Posted January 14, 2012 how about the main page that calls the file and the template what does it look like Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307676 Share on other sites More sharing options...
SyntheticShield Posted January 14, 2012 Author Share Posted January 14, 2012 how about the main page that calls the file and the template what does it look like Do you mean that actual code structure? Sorry, dont want to sound like a complete doofus, just want to make sure I answer accurately. Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307677 Share on other sites More sharing options...
blacknight Posted January 14, 2012 Share Posted January 14, 2012 yea the code so we can give you an answer lol Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307680 Share on other sites More sharing options...
litebearer Posted January 14, 2012 Share Posted January 14, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307689 Share on other sites More sharing options...
AyKay47 Posted January 14, 2012 Share Posted January 14, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307690 Share on other sites More sharing options...
SyntheticShield Posted January 14, 2012 Author Share Posted January 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307695 Share on other sites More sharing options...
blacknight Posted January 14, 2012 Share Posted January 14, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307697 Share on other sites More sharing options...
SyntheticShield Posted January 14, 2012 Author Share Posted January 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307700 Share on other sites More sharing options...
AyKay47 Posted January 14, 2012 Share Posted January 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307704 Share on other sites More sharing options...
SyntheticShield Posted January 14, 2012 Author Share Posted January 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307707 Share on other sites More sharing options...
AyKay47 Posted January 14, 2012 Share Posted January 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307709 Share on other sites More sharing options...
SyntheticShield Posted January 14, 2012 Author Share Posted January 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307712 Share on other sites More sharing options...
blacknight Posted January 15, 2012 Share Posted January 15, 2012 yea i forgot my $ infront of what_page lol Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307721 Share on other sites More sharing options...
xtremey_ytinasni Posted January 15, 2012 Share Posted January 15, 2012 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.. Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307727 Share on other sites More sharing options...
xtremey_ytinasni Posted January 15, 2012 Share Posted January 15, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307728 Share on other sites More sharing options...
SyntheticShield Posted January 15, 2012 Author Share Posted January 15, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307767 Share on other sites More sharing options...
jonsjava Posted January 15, 2012 Share Posted January 15, 2012 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307772 Share on other sites More sharing options...
laffin Posted January 15, 2012 Share Posted January 15, 2012 <?php header('Content-Type: text/plain'); echo basename($_SERVER['PHP_SELF']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/255031-detecting-page-to-set-page-title/#findComment-1307789 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.