Jump to content

Can PHP detect name of page


The14thGOD

Recommended Posts

I was wondering, I have a navigation on my site, that is pretty long, and it would probably change over time, so I wanted to make it an include. The problem is however, I need to detect on certain pages variables that I send to the URL. I need to do this in order to give a certain part of the navigation a new class so that it is highlighted.

 

So basically, is there a way for PHP to detect which page is loaded (ie. index.php, about.php, gallery.php)?

 

Could someone perhaps point me in the right direction?

 

Thanks,

 

The14thGOD

Link to comment
Share on other sites

You could either close and open php, or use heredoc.

 

Example for the "open & close":

<?php
//Some regular php...
$a = 1;
$b = $a + 1;

//Let's stop to print some html!
?>
<tag>
</tag>
<?php
//We can continue with our php 
echo $a;
?>

 

Heredoc

Heredoc is a way to output, just like you can use double quotes or single quotes. More info here.

Example:

<?php
//We want to echo some html!
echo <<<HEREDOC
<html>
<head>
<title>Hi!</title>
</head>
<body>

</body>
</html>
HEREDOC;
?>

 

 

Hope it helps,

Orio

Link to comment
Share on other sites

i faced similar problem some time before and i solved it using the following code.

to access which page is being displayed use 

      $url=getenv("SCRIPT_NAME"); //this will simply assign the whole path of the page to URL variable

and to check the exact page i just used:

      if(strcmp(substr($url,-9,9),"index.php")==0)

      {

              //code goes here....

      }

but the 9 and -9 depends on the length of the url

 

it may help

Link to comment
Share on other sites

Or you can simply do

 

<?php 

echo '<table >

<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>Some Text</td></tr>
<tr><td>'.$some_variable.'</td></tr>
</table>'

?>

 

But I prefer to open and close php as in the first example.

 

 

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.