douang2297 2012-10-17 13:33
浏览 69

如何使用多个命令创建一个导航

I am trying to build a navigation that determines the page the user in on and gives that link a different command to show a div tag if you click it on the current page. Example: you click on the home page to go to the home page. if you are on the home page and you click home again it causes a div tag to fade in. I have the div tag working but I don't know how to apply more than one command to an tag. I want the navigation to just be a sitewide include incase I have to make further updates to the site. Right now I have individual navigation on each page.

  • 写回答

3条回答 默认 最新

  • doucuo8618 2012-10-17 13:37
    关注

    In PHP you can learn about the page that has been requested by using the value in $_SERVER['REQUEST_URI'] and related entries.

    You can then take it to decide on which page you are and change the display of the menu according to the requested page.

    This allows you to include the menu template from many different files regardless where inside your page structure these are located.

    A short example, here with your suggested PHP_SELF entry and a non-directory structure:

    <?php
    
        $currentPage = basename($_SERVER["PHP_SELF"]);
    
        /** define the menu **/
        $menu = array(
            array('index.php', 'home');
        );
    
        /** process the menu **/    
        foreach ($menu as &$entry) {
            list($page, $name) = $entry;
            $isCurrent = $currentPage === $page;
            if ($isCurrent) {                
                $href  = '#';
                $extra = ' onclick="MM_effectAppearFade(display, 2500, 0, 100, true)"';
            } else {
                $href  = $page;
                $extra = '';
            }
            $entry = array_merge($entry, array($href, $extra));
        }
        unset($entry);
    
        /** output the menu **/
    ?>
    <ul>
        <?php foreach($menu as $entry) { ?>
            <li>
                <?php
                    list($page, $name, $href, $extra) = $entry;
                    printf(
                        '<a href="%s"%s>%s</a></li>', 
                        $href, $extra, htmlspecialchars($name)
                    );
                 ?>
            </li>
        <?php } ?>
    </ul>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里