Page 1 of 1

Multidimensional Arrays

Posted: Sat May 19, 2007 6:25 pm
by aspekt9
Here's what I'm trying to do. Search a given directory for all the pdf files. They're all stored with names of dates for each file. The format is mmddyyyy.pdf. Is what I did was put all the pdf files in the directory into a multidimensional array with their filename, day, month, and year. Now I want to insert them as links into a calendar that I have. The format for the calendar to add linked days looks like this:

Code: Select all

$days = array(
        2=>array('01022007.pdf','linked-day'),
        3=>array('01032007.pdf','linked-day'),
        8=>array('01082007.pdf','linked-day'),
        22=>array('01222007.pdf','linked-day'),
    );
The first number is the day of that month: 2,3,8,22. The 01022007.pdf etc is the link to the file. and linked-day represents its a day that has an event on it and is linked.

Code: Select all

Array
(
    [2] => Array
        (
            [0] => 01022007.pdf
            [1] => linked-day
        )
)
This array is a small piece of the $days array listed above, displaying the 2nd day link.
Now what I need to do is find all the files that have a day in each month. So all the days in month '01' and sort them together. Then I need to create an array like the one above from the multidimensional array:

Code: Select all

Array
(
    [] => Array
        (
            [filename] => 09112006.pdf
            [day] => 11
            [month] => 09
            [year] => 2006
        )

    [1] => Array
        (
            [filename] => 11212006.pdf
            [day] => 21
            [month] => 11
            [year] => 2006
        )

    [2] => Array
        (
            [filename] => 05022007.pdf
            [day] => 02
            [month] => 05
            [year] => 2007
        )
)
Here's the code to put all the files into an multidimensional array:

Code: Select all

$dir = 'notices';
if ($handle = opendir($dir)) {
	while (false !== ($file = readdir($handle))) {
              if ($file == (substr($file, -3) == 'pdf')) {
                        if (is_dir("$dir/$file")) {
                                $folder_array[] = strtolower($file);
                        } else {
                                $file_array[$i]['filename'] = strtolower($file);
                                $file_array[$i]['day'] = substr($file, 2,2);
                                $file_array[$i]['month'] = substr($file, 0,2);
                                $file_array[$i]['year'] = substr($file, 4, 4);                           
                         }
                }
                $i ++; 
	}
	closedir($handle);
}
Any help is greatly appreciated I'm trying to finish up this project and this is the last part.

Posted: Sat May 19, 2007 7:53 pm
by bdlang
Ah, phpcalendar. I know it well. :)

My one question to you would be; do you have to grab all files for all years at once or just the files for that month? In other words, does your script display only a month at a time or all 12 months?

When I built links for that from SQLite data, I pulled down data based on each month and filtered based on the month first. Start with the month, grab file names / day values based on the month, create an array like

Code: Select all

Array (
    1 => Array (
        5 => '01052007.pdf',
        7 => '01072007.pdf'
    )
)
Since each one of these is presumed to be a linked-day, you don't necessarily need to add that to the array.

Posted: Sun May 20, 2007 3:28 pm
by aspekt9
Thanks for the reply, good to find someone familiar with Keith's calendar haha. I have it set to display all 12 months but I had problems with the loop he provides. It would display all months but when I went to link say 1 => blah for day 1 it would link every 1st day in each month. So I had to print each month separately and use a $days array like $days1 $days2 etc. for each month. It's a pain is there an easier way to accomplish this in a loop? Also, do you think my way of storing all the files and breaking them up into a multidimensional array is the best way to approach this? Thanks again for the help.


Also, how would I put all the files with days in the same month together so an array for January would show every day that has a file for it?

Posted: Sun May 20, 2007 3:50 pm
by bdlang
Tried to reply earlier, the forum was t o o s l o w to respond for some reason.

At any rate, I looked at the code again and refreshed my memory as to how the links are created, so in essence the array has to include 'linked-day' as the class name as you originally had it, unless of course you want to tinker with Keith's code, but then you lose functionality in any other scripts that call that calendar script.

Since you're displaying the entire year, you can do as I suggested and group month data together, creating an overall array like

Code: Select all

Array (
    1 => Array (
        5 => array( '01052007.pdf', 'linked-day' ),
        7 => array( '01072007.pdf', 'linked-day' )
    ),

    3 => Array (
        12 => array ( 3-12 data here ),
        24 => array ( 3-24 data here )
    ),

    7 => Array (
        more of the same
    )
)
When you cull the filenames from your code, of course search by month first, then day, then populate the array accordingly. IF a filename exists with that month, it becomes part of the overall data structure. Then IF the day exists, etc.

Finally, when using the loop to create the entire year calendar set, on each iteration check that data array for the existing month (it'll be the key), e.g.

Code: Select all

// example, using $file_array as your overall data array
// modified a code snippet at http://keithdevens.com/software/php_calendar

<table style="margin: auto;">
<tr>

    <?php for( $month=1; $month<=12; $month++ ){ ?>

        <td>

           <?php $days= ( isset($file_array[$month]) ) ? $file_array[$month] : array(); ?>

            <?php echo generate_calendar($year, $month, $days, 3); ?>

        </td>

        <?php if($month%3 == 0 and $month<12){ ?>

            </tr><tr>

        <?php } ?>

    <?php } ?>

</tr>
</table>
Notice this bit here

Code: Select all

<?php $days= ( isset($file_array[$month]) ) ? $file_array[$month] : array(); ?>
That's the line that determines if that iteration's month's calendar has any links in it. If so, the $days array is filled with that data, if not, an empty array passes to generate_calendar().

Posted: Mon May 21, 2007 1:56 pm
by aspekt9
Got everything working, however how does it recognize what year it is? If I change the years to 2005 it still displays the linked files from 2007 on the calendar. So how do I designate I only want file names with the current year to display? Thanks again for the help.

I've tried adding another array for each year like this:

Code: Select all

$file_array[substr($file, 4,4)]][$pmonth][$pday] = array($dir."/".strtolower($file), 'linked-day');
It breaks the array up by each year but all the days turn unlinked. Probably because the calendar doesn't read the arrays like that?

Posted: Tue May 22, 2007 9:12 am
by bdlang
aspekt9 wrote:Any ideas bdlang?
:D

I'm sorry, the last time I saw this thread I thought you had it under control. I'll have some time later to take a look. ;)

Posted: Wed May 23, 2007 7:57 pm
by aspekt9
Ok, thanks.