PHP:目录导航菜单

发布于 2024-10-18 03:25:45 字数 519 浏览 3 评论 0原文

我有一个问题,但无法找到答案/脚本。我刚刚学习使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

属性 2024-10-25 03:25:45

有很多 php 函数可以迭代目录。我认为最干净的是使用 PHP SPL(标准 PHP 库)的目录迭代器。

http://www.php.net/manual/en/class.directoryiterator.php

$dir = new DirectoryIterator('about');
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
        // Can make the link here
        echo $fileinfo->getFilename();
    }
}

优点是你有很多可用的类函数。

如果您需要的不仅仅是文件名,可以使用:

  • 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

$dir = new DirectoryIterator('about');
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
        // Can make the link here
        echo $fileinfo->getFilename();
    }
}

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.

拥抱影子 2024-10-25 03:25:45
foreach (glob('*.php') as $filename) {
    echo $filename; //Make menu item here
}

但是,您可能不应该根据您的文件系统创建菜单。我建议您使用模板引擎,例如 Smarty

foreach (glob('*.php') as $filename) {
    echo $filename; //Make menu item here
}

However, you should probably not create a menu based on your filesystem. I suggest you use a template engine, like Smarty.

带上头具痛哭 2024-10-25 03:25:45

当然可以。我使用此方法查找目录中的所有文件并将文件名和扩展名添加到数组中。通过 while 循环的魔力,我可以为每个文件做一些事情。

$files = scandir('directory/of/files/');

这将创建一个名为 $files 的新数组。您可以使用 while 或 for 每个循环。在这种情况下我对每个都使用,因为它是最简单的。

foreach ($files as $value)
{
  $file = explode('.',$value); // Explode splits a variable by whatever

现在您已经有了文件和文件的实际名称,您可以将它们添加到导航中。我会将文件名添加到变量中。

  $navigation .= $file['0']; // Adds the file's name to the navigation variable
}

您可以将 $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.

    $files = scandir('directory/of/files/');
    

    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.

    foreach ($files as $value)
    {
      $file = explode('.',$value); // Explode splits a variable by whatever
    

    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.

      $navigation .= $file['0']; // Adds the file's name to the navigation 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 ;)

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文