动态菜单系统
我正在开发动态菜单系统,因为我构建的网站有严格的命名约定。
例如,如果我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然还没有测试过,但它应该可以工作。 ^^
编辑:
已修复,但在获取 $submenu 时。某处也不太可能出现“相差 1”错误。
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.
在我看来,你最好在“.”上爆发。而不是使用 strpos 和正则表达式。
It seems to me that you might be better off exploding on '.' instead of using strpos and regex.