Jump to content

[SOLVED] Page Names?


SkyRanger

Recommended Posts

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

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.