我在数据库中有一个表,其中包含了我网站上各种页面的路径。每个路径只列出一次。目前,我使用非常冗长和复杂的查询和PHP代码来获取所有这些数据,并将其重写为无序列表(以创建网站菜单)。似乎可能存在一种相对简单的循环方法,可以更有效地工作,但我似乎无法让任何东西工作。我找到了大量的PHP脚本,可以从文件树创建UL列表,但它们都不起作用或不能处理我的查询结果的固有非递归性质(有些需要我的路径的多维数组,这是可以接受的,除了我创建这些的麻烦)。我确实找到了一个脚本,它几乎可以正常工作,但它通过在
以下是样例:
DB将以下内容返回给结果数组:
我希望你能够帮我创建以下输出:中。但是在这个脚本中,先关闭父 ,然后插入一个
有没有想法在哪里添加额外的逻辑以及我如何实现这个目标?其他更好的方法吗?似乎这是一个常见的问题,应该有一种简单/标准的处理方式。也许如果我能弄清楚如何从我的路径中创建多维数组,那么就可以迭代这些数组来使其工作?
编辑:更加复杂 :-( 我尝试了casablanca的回答,它完美地解决了我的问题...但我意识到现在我需要跟进,使事情更加困难。为了显示页面的“名称”,我还需要在数组中拥有该信息,因此路径可能更适合用作数组键,而名称则作为数组的值。对于这种改变有任何想法吗?
<li>
部分之外放置子列表来格式化<ul>
部分(我将在下面解释)。以下是样例:
DB将以下内容返回给结果数组:
about/contact/
about/contact/form/
about/history/
about/staff/
about/staff/bobjones/
about/staff/sallymae/
products/
products/gifts/
products/widgets/
我希望你能够帮我创建以下输出:
我想要创建以下输出:
<ul>
<li>about/
<ul>
<li>about/contact/
<ul>
<li>about/contact/form/</li>
</ul>
</li>
<li>about/history/</li>
<li>about/staff/
<ul>
<li>about/staff/bobjones/</li>
<li>about/staff/sallymae/</li>
</ul>
</li>
</ul>
</li>
<li>products/
<ul>
<li>products/gifts/</li>
<li>products/widgets/</li>
</ul>
</li>
</ul>
我找到了一个脚本,链接在这里:http://www.daniweb.com/forums/thread285916.html。虽然这个脚本可以实现功能,但是它创建的UL列表格式不正确。在正确的情况下,子列表应该包含在父元素的
- 块。总体而言,这个脚本的设计相当优雅,可以很好地保持与层次结构相关的信息,但我无法理解它的细节以修复它。以下是完整的脚本函数:
function generateMainMenu()
{
global $db;
$MenuListOutput = '';
$PathsArray = array();
$sql = "SELECT PageUrlName FROM `table`";
$result = mysql_query($sql, $db) or die('MySQL error: ' . mysql_error());
while ($PageDataArray = mysql_fetch_array($result))
{
$PathsArray[] = rtrim($PageDataArray['PageUrlName'],"/"); //this function does not like paths to end in a slash, so remove trailing slash before saving to array
}
sort($PathsArray);// These need to be sorted.
$MenuListOutput .= '<ul id="nav">'."\n";//get things started off right
$directories=array ();
$topmark=0;
$submenu=0;
foreach ($PathsArray as $value) {
// break up each path into it's constituent directories
$limb=explode("/",$value);
for($i=0;$i<count($limb);$i++) {
if ($i+1==count($limb)){
// It's the 'Leaf' of the tree, so it needs a link
if ($topmark>$i){
// the previous path had more directories, therefore more Unordered Lists.
$MenuListOutput .= str_repeat("</ul>",$topmark-$i); // Close off the Unordered Lists
$MenuListOutput .= "\n";// For neatness
}
$MenuListOutput .= '<li><a href="/'.$value.'">'.$limb[$i]."</a></li>\n";// Print the Leaf link
$topmark=$i;// Establish the number of directories in this path
}else{
// It's a directory
if($directories[$i]!=$limb[$i]){
// If the directory is the same as the previous path we are not interested.
if ($topmark>$i){// the previous path had more directories, therefore more Unordered Lists.
$MenuListOutput .= str_repeat("</ul>",$topmark-$i);// Close off the Unordered Lists
$MenuListOutput .= "\n";// For neatness
}
// (next line replaced to avoid duplicate listing of each parent)
//$MenuListOutput .= "<li>".$limb[$i]."</li>\n<ul>\n";
$MenuListOutput .= "<ul>\n";
$submenu++;// Increment the dropdown.
$directories[$i]=$limb[$i];// Mark it so that if the next path's directory in a similar position is the same, it won't be processed.
}
}
}
}
$MenuListOutput .= str_repeat("</ul>",$topmark+1);// Close off the Unordered Lists
return $MenuListOutput."\n\n\n";
}
它会返回类似于这样的内容:
<ul id="nav">
<li><a href="/about">about</a></li>
<ul>
<li><a href="/about/history">history</a></li>
<li><a href="/about/job-opportunities">job-opportunities</a></li>
<li><a href="/about/mission">mission</a></li>
<li><a href="/about/privacy-policy">privacy-policy</a></li>
</ul>
<li><a href="/giftcards">giftcards</a></li>
<li><a href="/locations">locations</a></li>
<ul>
<li><a href="/locations/main-office">main-office</a></li>
<li><a href="/locations/branch-office">branch-office</a></li>
</ul>
<li><a href="/packages">packages</a></li>
</ul>
有没有想法在哪里添加额外的逻辑以及我如何实现这个目标?其他更好的方法吗?似乎这是一个常见的问题,应该有一种简单/标准的处理方式。也许如果我能弄清楚如何从我的路径中创建多维数组,那么就可以迭代这些数组来使其工作?
编辑:更加复杂 :-( 我尝试了casablanca的回答,它完美地解决了我的问题...但我意识到现在我需要跟进,使事情更加困难。为了显示页面的“名称”,我还需要在数组中拥有该信息,因此路径可能更适合用作数组键,而名称则作为数组的值。对于这种改变有任何想法吗?
$paths = array(
"about/contact/ " => "Contact Us",
"about/contact/form/ " => "Contact Form",
"about/history/ " => "Our History",
"about/staff/ " => "Our Staff",
"about/staff/bobjones/ " => "Bob",
"about/staff/sallymae/ " => "Sally",
"products/ " => "All Products",
"products/gifts/ " => "Gift Ideas!",
"products/widgets/ " => "Widgets"
);
然后在buildUL
函数中使用类似以下这行代码:
echo '<a href="'.$prefix.$key.'/">'.$paths[$prefix.$key].'</a>';
</li>
标签。它们在 HTML 中是可选的,删除它们将导致元素自动按正确的层次排序。 - Nisse Engström