PHP:目录导航菜单
我有一个问题,但无法找到答案/脚本。我刚刚学习使用 PHP。我使用 Perch 作为 CMS,到目前为止进展顺利。
在添加新页面时我遇到了障碍。我希望 PHP 能够为该目录创建动态导航菜单。
例如,我的“about”目录中有三个页面。
<代码>根 /关于 /index.php - page2.php - page3.php
我希望能够仅基于该目录输出侧面导航菜单。
Home - Page2 - Page3
当客户/用户创建新页面时,它会自动将其添加到列表中。所以...
root /about /index.php - page2.php - page3.php - newPage.php
...创建...
Home - Page2 - Page3 - New Page
code>
任何人都可以指出我的脚本方向或帮助我开始吗?
谢谢!
I have a question that I haven't been able to find the answer/script to. I'm just learning to use PHP. I'm using Perch as a CMS and has been going great so far.
I've run into a snag when it comes to adding new pages. Something that I want PHP to do is be able to create a dynamic navigation menu for only that directory.
For example, I have three pages in my 'about' directory.
root
/about
/index.php
- page2.php
- page3.php
I want to be able to output a side navigation menu based off only that directory.
Home - Page2 - Page3
And when the client/user creates a new page, it'll automatically add it to the list. So...
root /about /index.php - page2.php - page3.php - newPage.php
...creates...
Home - Page2 - Page3 - New Page
Can anyone point me into a direction of a script or help me get started?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有很多 php 函数可以迭代目录。我认为最干净的是使用 PHP SPL(标准 PHP 库)的目录迭代器。
http://www.php.net/manual/en/class.directoryiterator.php
优点是你有很多可用的类函数。
如果您需要的不仅仅是文件名,可以使用:
getPathname()
getPath()
getBasename()
isDir()
>等等...请参阅文档了解所有可能性。
There are quite a few php functions to iterate through directories. I think the cleanest is using PHP SPL (Standard PHP Library)'s Directory Iterator.
http://www.php.net/manual/en/class.directoryiterator.php
The advantage is you have a lot of class functions available to you.
If you need more than the filename you can use:
getPathname()
getPath()
getBasename()
isDir()
And so on... see the docs for all the possibilities.
但是,您可能不应该根据您的文件系统创建菜单。我建议您使用模板引擎,例如 Smarty。
However, you should probably not create a menu based on your filesystem. I suggest you use a template engine, like Smarty.
当然可以。我使用此方法查找目录中的所有文件并将文件名和扩展名添加到数组中。通过 while 循环的魔力,我可以为每个文件做一些事情。
这将创建一个名为 $files 的新数组。您可以使用 while 或 for 每个循环。在这种情况下我对每个都使用,因为它是最简单的。
现在您已经有了文件和文件的实际名称,您可以将它们添加到导航中。我会将文件名添加到变量中。
您可以将
$file['0']
包装在或
中,并为导航添加适当的样式。我建议在 Google 上搜索一些奇特的导航示例。我使用无序列表并使用 CSS 设置它们的样式。
尝试使用 foreach 循环和样式来获得完美的导航;)
Sure can. I use this method to find all files in a directory and add the file's name and extension into an array. Through the magic of a while loop, I can do something for each file.
This creates a new array named $files. You can use a while or for each loop. I use for each in this case, since it's the easiest.
Now that you have the files and the actual name of the files, you can add them to your navigation. I would add the filename to a variable.
You can wrap the
$file['0']
in a<span>
or<li>
and add appropriate styling for your navigation. I recommend searching Google for some fancy navigation examples. I use unordered lists and just style them with CSS.Play around with the foreach loop and styling to get the perfect navigation ;)