drxv39706 2010-08-11 19:56
浏览 18
已采纳

获取“活动”页面以设置导航选项卡

I want to be able to pull the name of the file and url I'm currently on, set two 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!

  • 写回答

1条回答 默认 最新

  • doukunsan5553 2010-08-11 20:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。