开发者

Generate nested list from filenames

I have a directory full of files with the naming convention of yyyymm.xml I need to generate a list like this:

<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>

My most recent attempt:

<?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);
}                   
?>


You could use something like this to split the filenames up:

$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;
}

Then you can list them like you need them:

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>';
}


  • Get a list of files in a directory
  • Get portions of a string
  • Arrays

Which part were you struggling on?

Essentially, you want to get a list of the files, loop over them and parse them into year and month, then you can stuff them in an array something like this:

$array[year][] = month;

This way you have an array which contains a list of years, each containing an array of months. Then go over that data structure to output your HTML.

I hope you don't expect people to just do your work for you. Work on this then when you encounter a specific problem, ask for help.

Edit: Looping the above structure

You've essentially got an array of arrays if you do as I've hinted at above. Your array structure would look like this:

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

To loop over this array

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

Which would output:

2010
jan
mar
sep
2009
feb
sep
oct

Sprinkle in HTML as needed.


<?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");
}
?>

Quick and dirty... ;-). There are a lot of things that have to be improved...
On base of that you should be able to build what you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜