2010-06-07 37 views
3

我正在使用嵌套的无序列表将用于jQuery插件mcdropdown的前端Web应用程序。使用PHP生成嵌套的ul列表的问题

下面是从PHP的数据结构:阵列的嵌套数组:

Array 
(
    [0] => Array 
     (
      [fullpath] => ../foil/alphanumeric/ 
      [depth] => 0 
     ) 

    [1] => Array 
     (
      [fullpath] => ../foil/alphanumeric/letters/ 
      [depth] => 1 
     ) 

    [2] => Array 
     (
      [fullpath] => ../foil/alphanumeric/numbers/ 
      [depth] => 1 
     ) 

    [3] => Array 
     (
      [fullpath] => ../foil/alphanumeric/numbers/symbols/ 
      [depth] => 2 
     ) 
) 

基本上,我把优良答案从this question on SO,改性有点:

global $fullpaths; // $fullpaths contains the above data structure in print_r 
$result = ''; 
$currentDepth = -1; 

while(!empty($fullpaths)) 
{ 
    $currentNode = array_shift($fullpaths); 

    if($currentNode['depth'] > $currentDepth) 
    { 
     $result .='<ul>'; 
    } 

    if($currentNode['depth'] < $currentDepth) 
    { 
     $result .=str_repeat('</ul>', $currentDepth - $currentNode['depth']); 
    } 

    $result .= '<li>'. $currentNode['fullpath'] .'</li>'; 

    $currentDepth = $currentNode['depth']; 

    if(empty($fullpaths)) 
    { 
     $result .= str_repeat('</ul>', 1 + $currentDepth); 
    } 
} 

print $result; 

,并得到了以下输出:

<ul> 
    <li>../foil/alphanumeric/</li> 
    <ul> 
     <li>../foil/alphanumeric/letters/</li> 
     <li>../foil/alphanumeric/numbers/</li> 
     <ul> 
      <li>../foil/alphanumeric/numbers/symbols/</li> 
     </ul> 
    </ul> 
</ul> 

mcdropdown无法接受jQuery插件,它希望这样的事情:

<li rel="1"> 
'Alphanumeric' 
    <ul> 
     <li rel="2">'Letters'</li> 
     <li rel="3">'Numbers' 
      <ul> 
       <li rel="4">'Symbols'</li> 
      </ul> 
     </li> 
    </ul> 
</li> 

坦率地说,我不太明白从问题的答案是如何工作的,我一直在试图修改的解决方案,以应付我的情况,但仍失败。

任何帮助和建议都是事先提供的。

回答

2

如果您已经拥有正确的深度值,那么您不需要递归。我有用于< UL>类似的功能 - < LI> -generation:

function ulli($newlevel, &$level, $UL="ul", $once=1) { 

    if ($level == $newlevel) { 
    echo "</li>\n"; 
    } 

    while ($level<$newlevel) { 
    $level++; 
    echo "\n <$UL>\n"; 
    } 

    while ($level>$newlevel) { 
    if ($once-->0) { echo "</li>\n"; } 
    $level--; 
    echo " </$UL>" 
    . ($level>0 ? "</li>" : "") . "\n"; // skip for final </ul> (level=0) 
    } 
} 

它需要用于参考的当前$级变量(= $ currentDepth)。你将它的深度传递给$ newlevel。然而,需要第一深度为1

基本用法是这样的:

$currentDepth=0; 
foreach ($array as $_) { 
    ulli($_["depth"]+1, $currentDepth); 
    echo "<li>$_[path]"; 
} 
ulli(0, $currentDepth); 

嘛,古怪。但它为我工作。

+0

,它为我的作品以及:)我会做一些修改,我相信mcdropdown jQuery插件可以然后和输出一起工作。非常感谢! – 2010-06-07 11:59:18

2

这段代码(缩进除外)产生你想要的结果吗?

$d = array(
    0 => array(
    'fullpath' => '../foil/alphanumeric/', 
    'depth' => 0 
    ), 

    1 => array(
    'fullpath' => '../foil/alphanumeric/letters/', 
    'depth' => 1 
    ), 

    2 => array(
    'fullpath' => '../foil/alphanumeric/numbers/', 
    'depth' => 1 
    ), 

    3 => array(
    'fullpath' => '../foil/alphanumeric/numbers/symbols/', 
    'depth' => 2 
    ) 
); 

echo "<ul>\n"; 
$cdepth = 0; $rel = 1; $first = true; $veryfirst = true; 
foreach($d as $e) 
{ 
    $mpath = "'" . ucfirst(basename($e['fullpath'])) ."'"; 
    if ($e['depth'] == $cdepth) { 
    if ($first && !$veryfirst) { echo "</li>\n";} 
    echo "<li rel=\"$rel\">", $mpath; 
    $rel++; $first = false; $veryfirst = false; 
    } else { 
    $depthdiff = $e['depth'] - $cdepth; 
    if ($depthdiff < 0) { 
     for($i = 0; $i < -$depthdiff; $i++) { 
    echo "</ul>\n</li>\n"; 
     } 
    } else { 
     for($i = 0; $i < $depthdiff; $i++) { 
    echo "\n<ul>\n"; 
    $first = true; 
     // indeed buggy if $depthdiff > 1... 
     } 
    } 
    echo "<li rel=\"$rel\">", $mpath, "\n"; 
    $rel++; $first = true; 
    } 
    $cdepth = $e['depth']; 
} 
for($i = 0; $i < $cdepth; $i++) { 
    echo "</ul>\n</li>\n"; 
} 
echo "</ul>\n"; 

编辑的代码:还不够完美,但你可以在上面......工作:d

+0

我已经这样做了,所以每一个深度的变化都会产生另一个李;如果来自更改关卡的新的ul必须是prev li的一部分,则必须修改一些代码。我没有在任何情况下对它进行测试(只是快速编码) – ShinTakezou 2010-06-07 10:03:34

+0

嗯,我有点sl,,现在我已经看到了预期的结果应该是什么;工作在一个新的代码... – ShinTakezou 2010-06-07 10:09:41

+0

感谢您的代码和您的更新。我刚刚下班回家,现在我也在为此工作:) – 2010-06-07 11:39:30