0

Sorry for the lengthy post that follows...

I have searched for a few days now on how to do this, and I cannot seem to wrap my head around how to get this done. I have a directory with twelve folders in it, one for each month of the year (January, February, etc...)

Inside each folder, I have a certain number of image files, desktop backgrounds. Each file is named according to the month it belongs to, the year the month it is for, whether it has a calendar or not, and it's resolution size. For example the Jan folder would have files with the naming convention: January2013_nocal_2560x1440; January2012_nocal_2560x1440; etc...

For the current month, such as May, there would also be files that are named: May2013_calendar_2560x1440; May2013_calendar_1920x1200; etc...

What I would like to do is create a list of the folders, in the order that each month follows in a year, starting with the current month and working backwards in time. Under each month, I would like to list the files in that folder that are for the correct year.

Here is what I would like output. For the Current Month:

<a class="menuitem" href="">May 2013</a>
<ul class="list2" style="float:left">Without Calendar:
    <li><a href="May/May2013_nocal_2560x1440.jpg">2560 x 1440</a></li>
    <li><a href="May/May2013_nocal_1920x1200.jpg">1920 x 1200</a></li>
    etc...
</ul>
<ul class="list2" style="float:right">With Calendar:
    <li><a href="May/May2013_calendar_2560x1440.jpg">2560 x 1440</a></li>
    <li><a href="May/May2013_calendar_1920x1200.jpg">1920 x 1200</a></li>
    etc...
</ul>

For each following month:

<a class="menuitem" href="">April 2013</a>
<ul class="list2" style="float:left">Without Calendar:
    <li><a href="April/April2013_nocal_2560x1440.jpg">2560 x 1440</a></li>
    etc...
</ul>
<a class="menuitem" href="">March 2013</a>
<ul class="list2" style="float:left">Without Calendar:
    <li><a href="March/March2013_nocal_2560x1440.jpg">2560 x 1440</a></li>
    etc...
</ul>
<a class="menuitem" href="">February 2013</a>
<ul class="list2" style="float:left">Without Calendar:
    <li><a href="February/February2013_nocal_2560x1440.jpg">2560 x 1440</a></li>
    etc...
</ul>
<a class="menuitem" href="">January 2013</a>
<ul class="list2" style="float:left">Without Calendar:
    <li><a href="January/January2013_nocal_2560x1440.jpg">2560 x 1440</a></li>
    etc...
</ul>
<a class="menuitem" href="">December 2012</a>
<ul class="list2" style="float:left">Without Calendar:
    <li><a href="December/December2012_nocal_2560x1440.jpg">2560 x 1440</a></li>
    etc...
</ul>
<a class="menuitem" href="">November 2012</a>
<ul class="list2" style="float:left">Without Calendar:
    <li><a href="November/November2012_nocal_2560x1440.jpg">2560 x 1440</a></li>
    etc...
</ul>

etc...

I want it to loop through each folder, in calendar order, and echo out each file in the folder for 2013, then loop through all folders again for 2012, and so on.

I have researched scandir, blob, explode and other resources to read/list each folder and file, but cannot figure out how to order everything how I want it to.

Any advice on how to do this would greatly be appreciated.

EDIT: I have been searching to no avail the past few days. I appreciate all the responses I have received so far, but none of them seem to actually do what I am wanting to do. Here is my base code so far, which pulls all of the folders/files I am wanting to sort and loop through.

$root = $_SERVER['DOCUMENT_ROOT']."/wallpapers/";

$folders = scandir($root);

foreach ($folders as $folder) {
if(is_dir($root . "/" . $folder) && $folder != "." && $folder != ".." && $folder !=    "images") {
    $months = array($folder);
    foreach ($months as &$month) {
        $files = scandir($root . $month);
        foreach ($files as $file) {
            if(is_file($root . $month . "/" . $file)) {
                echo $month . "/" . $file . "<br />";
            }
        }
    }
} 
} 

I am just not sure where to go from here.

Justin
  • 1
  • 1
  • on this site, you need to give some sort of initial effort to post a code sample of what you are trying to do. Then people can identify and help correct your mistakes. Please post you initial php code even if just a simple for loop – Zak May 02 '13 at 18:14
  • Sorting function after you get the folders names http://stackoverflow.com/questions/12424968/php-re-order-array-of-month-names – Fabi May 02 '13 at 18:34
  • have you seen this already? http://stackoverflow.com/questions/884974/sort-and-display-directory-list-alphabetically-using-opendir-in-php – reikyoushin May 02 '13 at 20:14
  • Are you using written english month names for SEO reasons or something? Otherwise I would structure this simple by using the year and under it the _number_ of the month (with a leading zero for month < 10) – makes sorting easier … – CBroe May 04 '13 at 02:42

2 Answers2

1

you can just list the files in the directory you want and then you can just use usort

Dima
  • 8,586
  • 4
  • 28
  • 57
0

This might be an example that will help get you started: http://www.weberdev.com/get_example.php3?ExampleID=4959 You may have to get the data then sort it for display.

Or see: Recursive browse all server directories and list newest created files with php

Community
  • 1
  • 1
captainhero70
  • 694
  • 6
  • 8