SkyRanger Posted April 23, 2007 Share Posted April 23, 2007 Is it possible to somehow read a page title based on the page you are one. for example: if the page is: mainpage.php so inside the mainpagephp it would say: include "pagetitles.inc"; and inside the the pagetitles.inc file it would say something like: if whateverpage echo $pagetitle = "Main Page"; Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/ Share on other sites More sharing options...
Dragen Posted April 23, 2007 Share Posted April 23, 2007 echo $pagetitle = $_SERVER["PHP_SELF"]; Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-235997 Share on other sites More sharing options...
SkyRanger Posted April 23, 2007 Author Share Posted April 23, 2007 echo $pagetitle = $_SERVER["PHP_SELF"]; ok, so how would I do that for multiple pages. ie: mainpage.php $pagetitle = "Main Page"; secondpage.php $pagetitle = "Second Page"; etc.... This would all be in a file called pagetitles.inc Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236004 Share on other sites More sharing options...
Dragen Posted April 23, 2007 Share Posted April 23, 2007 $page = $_SERVER["PHP_SELF"]; if($page == main.php){ $pagetitle = Main Page; }elseif($page == other.php){ $pagetitle = other page; }elseif($page = another.php){ $pagetitle = another page); } Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236043 Share on other sites More sharing options...
SkyRanger Posted April 23, 2007 Author Share Posted April 23, 2007 Ok, I created a file called pagesnames.inc with the following code: <? $page = $_SERVER["PHP_SELF"]; if($page == officemembers.php){ $pagetitle = "Office Members"; }elseif($page == sendpm.php){ $pagetitle = "Send Message"; }elseif($page = privatemessages.php){ $pagetitle = "Private Messages"; } ?> In the page officemembers.php i put include "pagenames.inc"; Somewhere in the page I put <?=$pagetitle; ?> <?= is because I have html code around it. I get the following errors: Notice: Use of undefined constant officemembers - assumed 'officemembers' in /home/prodco/public_html/members/pagetitles.inc on line 5 Notice: Use of undefined constant php - assumed 'php' in /home/prodco/public_html/members/pagetitles.inc on line 5 Notice: Use of undefined constant sendpm - assumed 'sendpm' in /home/prodco/public_html/members/pagetitles.inc on line 7 Notice: Use of undefined constant php - assumed 'php' in /home/prodco/public_html/members/pagetitles.inc on line 7 Notice: Use of undefined constant privatemessages - assumed 'privatemessages' in /home/prodco/public_html/members/pagetitles.inc on line 9 Notice: Use of undefined constant php - assumed 'php' in /home/prodco/public_html/members/pagetitles.inc on line 9 Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236068 Share on other sites More sharing options...
per1os Posted April 23, 2007 Share Posted April 23, 2007 if($page == 'officemembers.php'){ $pagetitle = "Office Members"; }elseif($page == 'sendpm.php'){ $pagetitle = "Send Message"; }elseif($page == 'privatemessages.php'){ $pagetitle = "Private Messages"; } You need to enclose words etc in "" or '' or else it tries to do a constant. Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236078 Share on other sites More sharing options...
Dragen Posted April 23, 2007 Share Posted April 23, 2007 I presume the lines the error is reffeering to are the if statements? just for good practice, the opening <? should always be <?php. why is it: <?=$pagetitle; ?> I'm not sure why the = is there.. surely it should be: <?php echo $pagetitle; ?> I've added an else statement at the end.. so that if $page doesn't equal any of the setpages it gives it a default name. <?php $page = $_SERVER["PHP_SELF"]; if($page == 'officemembers.php'){ $pagetitle = "Office Members"; }elseif($page == 'sendpm.php'){ $pagetitle = "Send Message"; }elseif($page == 'privatemessages.php'){ $pagetitle = "Private Messages"; }else{ $pagetitle = "unknown"; } ?> Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236082 Share on other sites More sharing options...
per1os Posted April 23, 2007 Share Posted April 23, 2007 I presume the lines the error is reffeering to are the if statements? just for good practice, the opening <? should always be <?php. why is it: <?=$pagetitle; ?> I'm not sure why the = is there.. surely it should be: ?> The <?=$string;?> Was a way that you could print strings out with PHP, it has been depreciated in PHP 5. Now the correct usage is what dragen put above. Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236086 Share on other sites More sharing options...
Dragen Posted April 23, 2007 Share Posted April 23, 2007 ah, thanks for clearing that up for me frost! Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236092 Share on other sites More sharing options...
SkyRanger Posted April 23, 2007 Author Share Posted April 23, 2007 hmm, ok, not sure why but all the of pages are showing "Private Messages" as the page title. And I changed all of the <?= to <?php echo Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236095 Share on other sites More sharing options...
Dragen Posted April 23, 2007 Share Posted April 23, 2007 what is the page name in address bar? $_SERVER["PHP_SELF"] simply takes the filename. Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236097 Share on other sites More sharing options...
per1os Posted April 23, 2007 Share Posted April 23, 2007 If you are still using this code: }elseif($page = 'privatemessages.php'){ $pagetitle = "Private Messages"; } That will always default to true due to the 1 equals sign. It is assigning $page = privatemessages.php, which should always return true. Make sure it is == the comparison operator. Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236100 Share on other sites More sharing options...
SkyRanger Posted April 23, 2007 Author Share Posted April 23, 2007 Fixed the problem I had to put /dir/filename.php for it to read properly $page = $_SERVER["PHP_SELF"]; I had: if($page == 'officemembers.php'){ $pagetitle = "Office Members"; }elseif($page == sendpm.php'){ ...... When it should have been: if($page == '/members/officemembers.php'){ $pagetitle = "Office Members"; }elseif($page == '/members/sendpm.php'){ ........ When I echoed $page it showed me the proper location. Also posted this for others who may want to use this. Thanks for all of you help Dragen and frost110 Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236125 Share on other sites More sharing options...
Dragen Posted April 23, 2007 Share Posted April 23, 2007 glad it's working! Link to comment https://forums.phpfreaks.com/topic/48276-solved-page-names/#findComment-236141 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.