动态菜单系统

发布于 2024-09-15 23:42:51 字数 963 浏览 0 评论 0原文

我正在开发动态菜单系统,因为我构建的网站有严格的命名约定。

例如,如果我有一个名为 AboutUs.php 的脚本,那么“关于我们”将是父菜单项。但是,如果我有一个名为 Product.Product1.php 的脚本,则“Product”是父项,“Product1”作为子菜单项。

这个想法是循环抓取我的前端文件夹中的所有脚本并构建一个数组,以便可以使用嵌套的 foreach 来回显 ul/li 元素,

我只是似乎无法获取 数组$submenu)正好..谢谢!

if ($handle = opendir('frontend/')) {
while(false !== ($sFile = readdir($handle))) {
    if (strrpos($sFile, ".php") === strlen($sFile)-strlen(".php")) {
        $pos1 = strrpos($sFile, ".");
        $menu = substr($sFile, 0, $pos1);
        $pos2 = strrpos($sFile, ".php");
        if ($pos1 == $pos2) { // "." and ".php" where in the pos, skip submenu
            $links[$menu] = 'fontend/'.$sFile;
        } else {
            $submenu = substr($sFile, $pos1, $pos2);
            $links[$menu][$submenu] = 'fontend/'.$sFile;
        }
    }
}

}

I am working on a dynamic menu system as the site I'm build has a strict naming convention.

For example, if I have a script are named AboutUs.php, then 'About Us' will be a Parent Menu Item. However if I have a script named Product.Product1.php then 'Product' is the Parent Item with 'Product1' as the Sub Menu Item.

The idea is to loop through grabbing all the scripts in my frontend folder and build an array so the ul/li elements can be echoed using a nested foreach

I just can't seem to get the Array and the $submenu) just right.. Thanks!

if ($handle = opendir('frontend/')) {
while(false !== ($sFile = readdir($handle))) {
    if (strrpos($sFile, ".php") === strlen($sFile)-strlen(".php")) {
        $pos1 = strrpos($sFile, ".");
        $menu = substr($sFile, 0, $pos1);
        $pos2 = strrpos($sFile, ".php");
        if ($pos1 == $pos2) { // "." and ".php" where in the pos, skip submenu
            $links[$menu] = 'fontend/'.$sFile;
        } else {
            $submenu = substr($sFile, $pos1, $pos2);
            $links[$menu][$submenu] = 'fontend/'.$sFile;
        }
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

恰似旧人归 2024-09-22 23:42:52

if ($handle = opendir('frontend/')) {
while(false !== ($sFile = readdir($handle))) {
    if (strrpos($sFile, ".php") === strlen($sFile)-strlen(".php")) {
        $posExt = strrpos($sFile, "." );
        $menu = substr($sFile, 0, $pos1);
        $posSub = strrpos($menu, ".");
        if ($posSub === false) { // "." and ".php" where in the pos, skip submenu
            $urls[$menu] = 'fontend/'.$sFile;
        } else {
            $submenu = substr($menu, $posSub, ($posExt-$posSub));
            $urls[$menu][$submenu] = 'fontend/'.$sFile;
        }
    }
}

虽然还没有测试过,但它应该可以工作。 ^^

编辑:
已修复,但在获取 $submenu 时。某处也不太可能出现“相差 1”错误。


if ($handle = opendir('frontend/')) {
while(false !== ($sFile = readdir($handle))) {
    if (strrpos($sFile, ".php") === strlen($sFile)-strlen(".php")) {
        $posExt = strrpos($sFile, "." );
        $menu = substr($sFile, 0, $pos1);
        $posSub = strrpos($menu, ".");
        if ($posSub === false) { // "." and ".php" where in the pos, skip submenu
            $urls[$menu] = 'fontend/'.$sFile;
        } else {
            $submenu = substr($menu, $posSub, ($posExt-$posSub));
            $urls[$menu][$submenu] = 'fontend/'.$sFile;
        }
    }
}

Haven't tested it, though, but it should work. ^^

EDIT:
Fixed but in getting the $submenu. It's not unlikely to be a "off-by-1" error somewhere as well.

春夜浅 2024-09-22 23:42:51

在我看来,你最好在“.”上爆发。而不是使用 strpos 和正则表达式。

while(false !== ($sFile = readdir($handle))) {
    if (strrpos($sFile, ".php") === strlen($sFile)-strlen(".php")) {
        $parts = explode('.', $sFile);
        if (count($parts) == 2)
        {
            $urls[$parts[0]] = 'frontend/'.$sFile;
        }
        else if (count($parts) == 3)
        {
            $urls[$parts[0]][$parts[1]] = 'frontend/'.$sFile;
        }
    }
}

It seems to me that you might be better off exploding on '.' instead of using strpos and regex.

while(false !== ($sFile = readdir($handle))) {
    if (strrpos($sFile, ".php") === strlen($sFile)-strlen(".php")) {
        $parts = explode('.', $sFile);
        if (count($parts) == 2)
        {
            $urls[$parts[0]] = 'frontend/'.$sFile;
        }
        else if (count($parts) == 3)
        {
            $urls[$parts[0]][$parts[1]] = 'frontend/'.$sFile;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文