RecursiveIteratorIterator和RecursiveDirectoryIterator到嵌套的html列表

时间:2012-05-28 05:00:23

标签: php recursion iterator html-lists

这是我的php脚本:

<?php

$path = $_SERVER['DOCUMENT_ROOT'].'/test';

$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

foreach($objects as $name => $object){  
    echo  $objects->getDepth() . " " . $object->getFilename() . "<br/>";
    }

?>

这是脚本迭代的目录/文件树。 (它位于一个名为$ _SERVER ['DOCUMENT_ROOT']的简单根目录中。'/ test'):

/food
/food/drinks
/food/drinks/water.html
/food/drinks/milk.html
/food/drinks/soda.html
/food/entrees
/food/entrees/hot
/food/entrees/hot/hamburger.html
/food/entrees/hot/pizza.html
/food/entrees/cold
/food/entrees/cold/icecream.html
/food/entrees/cold/salad.html
/cosmetics
/cosmetics/perfume
/cosmetics/perfume/chic.html
/cosmetics/perfume/polo.html
/cosmetics/perfume/lust.html
/cosmetics/lipstick
/cosmetics/lipstick/colors
/cosmetics/lipstick/colors/red.html
/cosmetics/lipstick/colors/pink.html
/cosmetics/lipstick/colors/purple.html

这是脚本输出的内容:

0 food
1 drinks
2 milk.html
2 water.html
2 soda.html
1 info.php
1 entrees
2 hot
3 pizza.html
3 hamburger.html
2 cold
3 ice_cream.html
3 salad.html
0 cosmetics
1 lipstick
2 colors
3 pink.html
3 red.html
3 purple.html
1 perfume
2 polo.html
2 lust.html
2 chic.html
0 error_log
0 test.php

忽略$ objects-&gt; getDepth()整数;它仅供参考

问题:如何修改我的脚本以输出嵌套的无序列表,如下所示:

<ul>
  <li>food</li>
    <ul>
      <li>drinks</li>
        <ul>
          <li>water.html</li>
          <li>milk.html</li>
          <li>soda.html</li>
        </ul>
      <li>entrees</li>
        <ul>
          <li>hot</li>
            <ul>
              <li>hamburger.html</li>
              <li>pizza.html</li>
            </ul>
          <li>cold</li>
            <ul>
              <li>icecream.html</li>
              <li>salad.html</li>
            </ul>      
        </ul>
    </ul>
  <li>cosmetics</li>
    <ul>
      <li>perfume</li>
        <ul>
          <li>chic.html</li>
          <li>polo.html</li>
          <li>lust.html</li>
        </ul>
      <li>lipstick</li>
        <ul>
          <li>colors</li>
            <ul>

谢谢!

2 个答案:

答案 0 :(得分:15)

这是一个使用DomDocument

基本思想是每个目录的内容由<ul>表示,目录中的每个元素由<li>表示
如果element是非空目录,则它将包含<ul>来表示其竞争等等。

$path = $_SERVER['DOCUMENT_ROOT'].'/test';
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
$dom = new DomDocument("1.0");
$list = $dom->createElement("ul");
$dom->appendChild($list);
$node = $list;
$depth = 0;
foreach($objects as $name => $object){  
    if ($objects->getDepth() == $depth){
//the depth hasnt changed so just add another li
        $li = $dom->createElement('li', $object->getFilename());
        $node->appendChild($li);
    }
    elseif ($objects->getDepth() > $depth){
//the depth increased, the last li is a non-empty folder 
        $li = $node->lastChild;
        $ul = $dom->createElement('ul');
        $li->appendChild($ul);
        $ul->appendChild($dom->createElement('li', $object->getFilename()));
        $node = $ul;
    }
    else{
//the depth decreased, going up $difference directories
        $difference = $depth - $objects->getDepth();
        for ($i = 0; $i < $difference; $difference--){
            $node = $node->parentNode->parentNode;
        }
        $li = $dom->createElement('li', $object->getFilename());
        $node->appendChild($li);
    }
    $depth = $objects->getDepth();
}
echo $dom->saveHtml();

输出将与

一致
<ul>
    <li>dir1</li>
    <li>dir2
        <ul>
            <li>in dir2</li>
        <ul>
    <li>file in root</li>
<ul> 

答案 1 :(得分:1)

将深度存储在变量中并检查变量是否已增加或减少每个循环?

如果深度已增加,则添加另一个<ul>标记,如果已减少,则添加结束</ul>标记。

每个循环总是输出<li>标签来包装文件名。

e.g。

$depth = 0;
foreach ($objects as $name => $object) {
    $depth2 = $objects->getDepth();
    if($depth2 > $depth) { echo '<ul>'; }
    echo "<li>" . $object->getFilename() . "</li>";
    if($depth2 < $depth) { echo '</ul>'; }
    $depth = $depth2;
}

这需要一些额外的测试和修改以适应您现有的代码,但希望它能为您提供一些想法。