开发者

Getting 'active' page to set nav tab

I want to be able to pull the name of the file and url I'm currently on, set t开发者_运维百科wo variables (filename, folder), and check it against an array. Depending what the values are, we set an 'active' class to that < li> to show the page is a part of that section.

I've never really used arrays before, but I did a little research thinking they would be the most logical way to do this. I setup an array with the folder names, and what pages should be there

$navPages = array ( "band" => array ("biography","index"),
                    "music" => array ("discography","lyrics","meanings",),
                    "site" => array ("about","team")
                  );

So above, the folder /band/ should have biography.php and index.php. So now, I need a way to look through that array and make a match so that if I'm visiting "site.com/band/biography.php" I'm able to to check the array and say "Yes, you're on this page. Now apply an 'active' class to the "Band" < LI>.

So far, I'm able to get the filename

$page = basename($_SERVER['PHP_SELF'], ".php");

Now, I can't figure out how to check each array to see if I'm in the right page. I tried

if(in_array($page, $navPages[band])) {
   $class = 'active';
}

And that works, but I figured there has to be way to check all 3 arrays, 'band', 'music', 'site' all at once. So i want to see if the filename is in the array, but also I need to make sure it's in the right place. For example, index.php can be in any folder, I only want a true statement if index.php is in the 'band' array, not just overall $navPages array.

Can someone help me out with this? I tried to be as detailed as possible, let me know if there's something I'm missing.

Also, this is the first time I try doing a nav section like this. So if you see that I'm missing some logic on what I need to make this work, any tips would be appreciated, Thank you!


Don't specify the key band:

if(in_array($page, $navPages)) {
   $class = 'active';
}

This will look for the current page into the entire array and if so, condition will set the class to current.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜