从文件名生成嵌套列表

时间:2010-12-06 12:15:30

标签: php date

我有一个目录,其中包含yyyymm.xml命名约定的文件 我需要生成一个这样的列表:

<ul>
 <li>2010
  <ul>
   <li>Dec</li>
   <li>Nov</li>
   ...
   <li>Feb</li>
   <li>Jan</li>
  </ul>
 </li>
 <li>2009
  <ul>
   <li>Dec</li>
    ...etc.
  </ul>
 </li>
</ul>

我最近的尝试:

<?php
$tempYear = 0;
date_default_timezone_set('Australia/Melbourne');
if ($handle = opendir('news')) {
   // Open the news dir
    echo "<p>News archive</p>";
    echo "<ul>";
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $fileDate = basename($file, ".xml");
            $fileDate = $fileDate . "01";
            // Strip the .xml extention from the filename
            $currYear = date("Y", strtotime($fileDate));
            $currMonth = date("F");
            $archMonth = date("F", strtotime($fileDate));
            //echo "<li>$archMonth</li>";
            if ($currYear != $tempYear){
                echo "<li>$currYear";
                $tempYear = $currYear;
            }else{
                echo "<ul>";
                while ($currMonth != $archMonth){
                    echo "<li>$archMonth</li>";
                }
                echo "</ul>";
                echo "</li>";
            }
        }
    }
    echo "</ul>";
    closedir($handle);
}                   
?>

3 个答案:

答案 0 :(得分:2)

您可以使用类似的东西来分割文件名:

$list = array();

foreach ($files as $filename) {
    $year = intval(substr($filename, 0, 4));
    $month = intval(substr($filename, 3, 2));

    if (!is_array($list[$year])) {
        $list[$year] = array();
    }

    $list[$year][] = $month;
}

然后你可以像你需要的那样列出它们:

if (!empty($list)) {
    echo '<ul>';

    foreach ($list as $year => $months) {
        echo '<li>'.$year;

        if (!empty($months)) {
            echo '<ul>';

            foreach ($months as $month) {
                // you can add some magic here to display
                // the month as text instead of a number.
                echo '<li>'.$month.'</li>';
            }

            echo '</ul>';
        }

        echo '</li>';
    }

    echo '</ul>';
}

答案 1 :(得分:0)

你在哪个部分挣扎?

基本上,你想获得一个文件列表,循环它们并将它们解析为年份和月份,然后你可以将它们填充到这样的数组中:

$array[year][] = month;

这样你就有了一个包含年份列表的数组,每个数组都包含一个月份数组。然后查看该数据结构以输出您的HTML。

我希望你不要指望别人为你做你的工作。当你遇到一个特定的问题时,可以解决这个问题,寻求帮助。

编辑:循环上述结构

如果你像我上面所暗示的那样,你基本上得到了一系列数组。您的数组结构如下所示:

$array['2010'] = array('jan','mar','sep');
$array['2009'] = array('feb','sep','oct');

循环遍历此数组

foreach($array as $key => $months) {
    echo "year: $key\n";
    foreach($months as $month) {
         echo "$month\n";
    }
}

哪个会输出:

2010
jan
mar
sep
2009
feb
sep
oct

根据需要使用HTML。

答案 2 :(得分:0)

<?php
$dir = '.';

/**
 * find the files and store them in array
 **/
if(is_dir($dir)) {
  if($dh = opendir($dir)) {
    $menutree = array();

    while(($file = readdir($dh)) !== false) {
      // check if filename is correct
      if(strlen($file) == 10 && strtolower(substr($file, -3)) == 'xml') {
        $menuTree[substr($file, 0, 4)][substr($file, 4, 2)] = $file;
      }
    }

    closedir($dh);
  }
}

/**
 * build the tree
 **/
if(is_array($menuTree) && count($menuTree) > 0) {
  // sort
  asort($menuTree);

  foreach($menuTree as $year => $months) {
    ksort($menuTree[$year]);
  }

  // define months
  $monthsWords = array(
    'Jan',
    'Feb',
    'Mar',
    'Apr',
    'May',
    'Jun',
    'Jul',
    'Sep',
    'Oct',
    'Nov',
    'Dec'
  );

  // echo html
  echo('<ul>' . "\n");

  foreach($menuTree as $year => $months) {
    echo("\t" . '<li>' . $year . "\n");
    echo("\t\t" . '<ul>' . "\n");

    foreach($months as $month => $filename) {
      echo("\t\t\t" . '<li>' . $monthsWords[(int) $month - 1] . '</li>' . "\n");
    }

    echo("\t\t" . '</ul>' . "\n");
    echo("\t" . '</li>'); 
  }

  echo('</ul>' . "\n");
}
?>

快速又脏...... ;-)。有很多事情需要改进......
在此基础上,你应该能够建立你需要的东西。