dongnao3990 2014-09-30 18:38
浏览 41
已采纳

将当前css应用于PHP中的导航

I am using a dynamic header for my page built in PHP and i am trying to apply the current CSS to the selected page. I can do this by defining class current in the html but this doesn't work with a dynamic header.

Here is my php header file what is wrong with my code. As far as I can tell the php statement should figure out the current page and apply the current css but it doesn't.

Thanks in advance for your help :). My website is: http://wonx.dk/phpintergrated/index.php

<?php
function get_current($title) {
  if (strpos($_SERVER['REQUEST_URI'], $title) !== false)
    echo 'class="current"';
}
?>


<a href="index.html"><img class="logo" src="img/logofinal.svg"  alt="Devil Sheep Logo" /></a>

<label for="menuon">
<img src="img/menu.svg" alt="MENU"></label>
<input type="checkbox" id="menuon">

<nav>
      <ul class="topmenu">
        <li><a href="index.php" title="Home" >Home</a></li> <!--Sets the home tab to on meaning it is selected.-->
        <li><a href="aboutme.php" title="About Me">About Me</a><!--Whitespace causes problems with display:inline-->
           <ul class="submenu">
            <li class="aboutdropdown"><a href="aboutme.php" title="About Me">About Me</a></li>
            <li><a href="portfolio.php" title="Portfolio">Portfolio</a></li>
            <li><a href="photogallery.php" title="Photo Gallery">Photo Gallery</a></li>
            </ul>
          </li>
        <li><a href="recipe.php" title="Recipe">Recipe</a></li>
        <li><a href="song.php" title="Song">Song</a></li>
        <li><a href="cv.php" title="CV">CV</a></li>
        <li><a href="experiments.php" title="Experiments">Experiments</a></li>
        <li><a href="contact.php" title="Contact Me">Contact Me</a></li>
      </ul>
</nav>
  • 写回答

3条回答 默认 最新

  • doufan9290 2014-09-30 18:59
    关注

    You have a finite number of menu items.

    declare an array with all your menu items

     $current['index.php'] = "";
     .
     .
     .
     $current['contact.php'] = "";
     $temp = $current;
     foreach($temp AS $key => $value)
     {
       if (strpos($_SERVER['REQUEST_URI'], $key) !== false)
        $current[$key] = 'class="current"';
     }
    

    Now go to the html

      <li <?php echo $current["index.php"]?>><a href="index.php" title="Home" >Home</a></li>
     .
     .
     .
     <li <?php echo $current["contact.php"]?>><a href="contact.php" title="Contact Me">Contact Me</a></li>
    

    The . . . mean rinse and repeat.

    Enjoy!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能