php中的动态导航 [英] dynamic navigation in php

查看:134
本文介绍了php中的动态导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在php中实现动态导航?

How do i implement a dynamic navigation in php?

例如

首页|关于|联系|常见问题教程

Home | about | contact | faq | tutorials

我需要动态地自动生成链接到每个页面,而无需脚本。例如我应该拥有所有的链接,而不需要手动输入每个其他页面的链接?

i need to automatically generate the links dynamically respectively to each page without much script. e.g i should have all the links without manually entering the links to each other page?

推荐答案

如果你只想显示一个菜单对于一组已知的页面而不重新构建您当前的代码,该如何做:

If you just want to display a menu for a known set of pages without re-architecting your current code, how about this:

<?php
$pages = array(
    'index.php' => 'Home',
    'about.php' => 'About',
    'contact.php' => 'Contact',
    'faq.php' => 'FAQ',
    'tutorials.php' => 'Tutorials',
) ;

$currentPage = basename($_SERVER['REQUEST_URI']) ;

?>

<div id="menu">
    <ul id="menuList">
        <?php foreach ($pages as $filename => $pageTitle) { 
            if ($filename == $currentPage) { ?>
        <li class="current"><?php echo $pageTitle ; ?></li>
            <?php } else { ?>
        <li><a href="<?php echo $filename ; ?>"><?php echo $pageTitle ; ?></a></li>
            <?php
            } //if 
         } //foreach 
            ?>
    </ul>
</div>

将其放在自己的文件中,说 menu.php ,然后将其包含在每个页面中。然后,您可以使用CSS 您的菜单样式

Put this in its own file, say menu.php, and then include it in each page. Then you can style your menu with CSS.

这篇关于php中的动态导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆