SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Baden, PA
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    __FILE__ or PHP SELF to detect what page user is on?

    I apologize if the thread title is crappy.

    Just as some background, so you guys know what I'm dealing with here:

    This is my first time working with a very simple PHP template system. Each of my site's pages includes a template file. The template file has all of the info for the header and footer of the page.

    The site is completely CSS/XHTML based and uses no tables.

    I've used the "Happy Cog method" of image "swapping" to create the rollover states in my main navigation.

    I want to be able to create a "You Are Here" effect by keeping the nav buttons highlighted while on their corresponding pages. (ex: you're on the links page and the links tab in the navigation menu is highlighted).

    In the past, I've done this by using a simple "body id=" tag in my HTML. The body id tells the CSS what page I'm on and the CSS highlights the correct nav button.

    Because my PHP template generates the header info and everything right up to the body content (including the nav menu), I can't hard code a body id into each and every page like I have in sites that I've done before.

    Is there a way I could use PHP to detect what page the user is on, thus highlighting what page the user is on?

    I'm guessing it's something like an if statement that might be like, "if page = links.php, then set body id=links, which would highlight the appropriate button.

    Unfortunately, I've never used __FILE__ or PHP SELF, but I think one of the two might be the solution to what I'm trying to accomplish. Heck, I don't even know if I'd be able to let PHP set a body id or insert something into a body tag.

    Thanks in advance for your help!

  2. #2
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It really depends how you have setup the page
    If you're using query strings, like index.php?act=something
    Then you can just do an if ($_GET['act'] == 'something')
    Or even better, use an ternary operator
    $_GET['act'] == 'something' ? : ' class="selected"' : ''

    Or you if you have a different file for every page, you could use $_SERVER['PHP_SELF']

    In both case, you're better off storing all your pages inside an array and display your menu like this:

    $pages = array('index', 'page1', 'page2');

    foreach ($pages as $page)
    {
    echo '<li'.($_GET['act'] == $page ? ' class="selected" : '').'</li>';
    }
    Speed & scalability in mind...
    If you find my reply helpful, fell free to give me a point

  3. #3
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Baden, PA
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry it took me a while to get back to this, but I'm going to try it right now.

    Thanks.

  4. #4
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    __FILE__ is just a fake example, a bit like $foo & $bar except they are variables and it is a constant.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •