Quick PHP question

Out of context: Reply #10

  • Started
  • Last post
  • 25 Responses
  • armsbottomer0

    i agree with acescence, if you are unsure about the structure of your nav, it might be a good idea to use an array to store your nav pages. this is especially useful if you're using subnavs.

    also, regarding the article , you can use the $_SERVER['SCRIPT_NAME'] variable to test if a nav matches the current page's file name, if the name of the name is the same as the file name.

    $title = basename($_SERVER['SCRIPT_NAME... '.php');

    if your file was named ladbroke.php, this would output "ladbroke". when you match with your nav name, you can use lcfirst() on your nav name to make sure its lowercase.

    you'll sometimes have to tweak the name of the nav your trying to match (like if your file was named "recentprojects.php" and you wanted to match with "Recent Projects") with basic regular expressions. so if your file is called recentprojects.php, you could do.

    $nav = array("Ladbroke, "Recent Projects");
    $title = basename($_SERVER['SCRIPT_NAME... '.php');
    $page = lcfirst(str_replace(" ", "", $nav[1]);
    if($page == $title){
    echo " id=\"currentpage\"";
    }
    ?>

    this method may seem like overkill, but if you're working with a bunch of pages and need to make a single change across multiple pages, its quite convenient.

    • $_SERVER['SCRIPT_NAM... '.php'); should be $_SERVER['SCRIPT_NAM... '.php');armsbottomer
    • Gah, i wish i understood what all this meant.JamesBoynton
    • arr... $_SERVER['SCRIPT_NAM... '.php');armsbottomer
    • it's actually not that difficult, regular expressions will always look like gibberish when you first approach them thougharmsbottomer
    • oh yeah, i screwed up, strtolower() would work better than lcfirst()armsbottomer

View thread