php 递归列表帮助
我正在尝试在 PHP 中显示我正在处理的网站的递归列表。
我在尝试显示第二个级别时确实遇到了麻烦。我有一个将内容显示到页面的功能,如下所示。
function get_menu_entries($content,$which=0)
{
global $tbl_prefix, $sys_explorer_vars, $sys_config_vars;
// INIT LIBRARIES
$db = new DB_Tpl();
$curr_time = time();
$db->query("SELECT * FROM ".$tbl_prefix."sys_explorer WHERE preid = '".$which."' && config_id = '".$sys_explorer_vars['config_id']."' && blocked = '0' && startdate < '".$curr_time."' && (enddate > '".$curr_time."' || enddate = '') ORDER BY preid,sorting");
while($db->next_record()){
$indent = $db->f("level") * 10 - 10;
$sitemap_vars['break'] = "";
$sitemap_vars['bold'] = "";
if($db->f("level") == 2) {
$sitemap_vars['ul_start'] = "";
$sitemap_vars['bold'] = "class='bold'";
$sitemap_vars['ul_end'] = "";
}
switch($db->f("link_type"))
{
case '1': // External Url
$sitemap_vars['hyperlink'] = $db->f("link_url");
$sitemap_vars['target'] = "";
if($db->f("link_target") != "") {
$sitemap_vars['target'] = "target=\"".$db->f("link_target")."\"";
}
break;
case '2': // Shortcut
$sitemap_vars['hyperlink'] = create_url($db->f("link_eid"),$db->f("name"),$sys_config_vars['mod_rewrite']);
$sitemap_vars['target'] = "";
break;
default:
$sitemap_vars['hyperlink'] = create_url($db->f("eid"),$db->f("name"),$sys_config_vars['mod_rewrite']);
$sitemap_vars['target'] = "";
break;
}
if($db->f("level") > 1) {
$content .= "<div style=\"text-indent: ".$indent."px;\" ".$sitemap_vars['bold']."><a href=\"".$sitemap_vars['hyperlink']."\" ".$sitemap_vars['target'].">".$db->f("name")."</a></div>\n";
}
$content = get_menu_entries($content,$db->f("eid"));
}
return(''.$content.'');
}
目前内容显示正常,但我想将此功能变成 DHTML 下拉菜单。 目前,2 级元素的情况是使用 CSS 来缩进内容。我需要做的是将 UL 标记放在 2 级元素的开头,将 /UL 标记放在末尾。
我希望这是有道理的。任何帮助将不胜感激。
I am trying to display a recursive list in PHP for a site I am working on.
I am really having trouble trying to get the second level to display. I have a function that displays the contents to the page as follows.
function get_menu_entries($content,$which=0)
{
global $tbl_prefix, $sys_explorer_vars, $sys_config_vars;
// INIT LIBRARIES
$db = new DB_Tpl();
$curr_time = time();
$db->query("SELECT * FROM ".$tbl_prefix."sys_explorer WHERE preid = '".$which."' && config_id = '".$sys_explorer_vars['config_id']."' && blocked = '0' && startdate < '".$curr_time."' && (enddate > '".$curr_time."' || enddate = '') ORDER BY preid,sorting");
while($db->next_record()){
$indent = $db->f("level") * 10 - 10;
$sitemap_vars['break'] = "";
$sitemap_vars['bold'] = "";
if($db->f("level") == 2) {
$sitemap_vars['ul_start'] = "";
$sitemap_vars['bold'] = "class='bold'";
$sitemap_vars['ul_end'] = "";
}
switch($db->f("link_type"))
{
case '1': // External Url
$sitemap_vars['hyperlink'] = $db->f("link_url");
$sitemap_vars['target'] = "";
if($db->f("link_target") != "") {
$sitemap_vars['target'] = "target=\"".$db->f("link_target")."\"";
}
break;
case '2': // Shortcut
$sitemap_vars['hyperlink'] = create_url($db->f("link_eid"),$db->f("name"),$sys_config_vars['mod_rewrite']);
$sitemap_vars['target'] = "";
break;
default:
$sitemap_vars['hyperlink'] = create_url($db->f("eid"),$db->f("name"),$sys_config_vars['mod_rewrite']);
$sitemap_vars['target'] = "";
break;
}
if($db->f("level") > 1) {
$content .= "<div style=\"text-indent: ".$indent."px;\" ".$sitemap_vars['bold']."><a href=\"".$sitemap_vars['hyperlink']."\" ".$sitemap_vars['target'].">".$db->f("name")."</a></div>\n";
}
$content = get_menu_entries($content,$db->f("eid"));
}
return(''.$content.'');
}
At the moment the content displays properly, however I want to turn this function into a DHTML dropdown menu.
At present what happens with the level 2 elements is that using CSS the contents are indented using CSS. What I need to happen is to place the UL tag at the beginning and /UL tag at the end of the level 2 elements.
I hope this makes sense. Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用带有缩进的
标签,而是为每个级别(包括第一个级别)使用无序列表。让您的函数在开头输出
,在结尾输出
,并更改
为简单的
。为第一层提供一个 ID,以便您可以连接到它。然后,您可以使用 CSS 删除项目符号点并更改缩进等。您不需要计算缩进或是否在 PHP 中将文本加粗,而是使用选择器并让浏览器来计算:
这一切都会允许您使用一种标准算法来生成列表,而不是根据级别进行分支,并使菜单看起来像网络爬虫、搜索引擎和无 CSS 浏览器的菜单。
顺便说一句,您确实应该
htmlspecialchars
处理所有动态文本。您不希望杂散的<
弄乱您的标记。Instead of using
<div>
tags with indentation, use an unordered list for each level, including the first one. Have your function output<ul>
at the start and</ul>
at the end, and change<div style="text-indent: ...">
to a simple<li>
. Give the first level an ID so you can hook onto it. Then you can use CSS to remove bullet points and change the indentation, etc. You don't need to calculate the indentation or whether to bold the text in PHP—instead, use selectors and allow the browser to figure it out:All this will allow you to use one standard algorithm for generating your list, instead of branching based on the level, as well as making the menu look like a menu to web crawlers, search engines and those with CSS-less browsers.
By the way, you should really be
htmlspecialchars
-ing all that dynamic text. You don't want a stray<
to mess up your markup.如果我错了,请纠正我,但您的问题似乎不是您不知道何时打印第一组
标记,而是您正在尝试打印每个函数调用中的第二组(对于级别 2),因此最终会出现太多的打开/关闭标记。
我认为你应该尝试做的是首先按级别对数组进行排序(请参阅 uasort() 然后你可以在第二级的第一个循环上输出你的开始
标签(使用计数器来跟踪你所在的循环,然后执行类似的操作
if ($i == 0)
或 if($i == (count($array) - 1))
无论嵌套多少层,这都有效。有。Correct me if I'm wrong, but it seems that your issue is not that you don't know when to print the first set of
<ul>
tags, but that you are trying to print the second set (for level 2) in every function call, and thus ending up with too many open/close tags.I think what you should try to do is sort your array by level, first (see uasort() and then you can output your opening
<ul>
tag on the first loop of the second level (use a counter to keep track of which loop you're on and then do something likeif ($i == 0)
or if($i == (count($array) - 1))
. This will work no matter how many nested levels you have.