2010-06-07 34 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