Calendar / Files in a Directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Calendar / Files in a Directory

Post by aspekt9 »

I want to locate all the files in a directory and import them into a calendar and link to them. So since the files are already named with the date such as 01102007.pdf. What would be the easiest way to incorporate it into my calendar. This is how I have my calendar setup: right now it pulls the dates from the database so I want it to now pull them from all the file names in the directory and link to them:

Code: Select all

<?php
include 'config.php';
include 'calendar.php';
$time = time();
$today = date('j',$time);
$cmonth = date('n');
    $events = mysql_query("SELECT *, DATE_FORMAT(date, '%M %e, %Y %h:%m:%s') as
            date,  DATE_FORMAT(date, '%c') as month,  DATE_FORMAT(date, '%e') as day FROM calevents order by date desc") or die(mysql_error());
        if($events)
          {
          $days = array();
          while($row = mysql_fetch_array($events))
            {
            if($row['month'] == $cmonth)
              {
              $day = $row['day'];
              $days[$day] = array('events.php?action=view&id='.$row['id'], 'linked-day');
              }
           }
        }    
//$days[$today] = array(NULL,NULL,'<span style="color: red; font-weight: bold;">'.$today.'</span>');
echo generate_calendar(date('Y', $time), date('n', $time), $days, 3);
?>
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

This script will list the contents of a directory:

Code: Select all

<?php
$d = dir("/path/to/the/pdfs");
while (false !== ($entry = $d->read())) {
   echo $entry."\n";
}
$d->close();
?>
If you want to get the date from $entry, you could use substr().
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

Alright I got it and sort them here was my code:

Code: Select all

<?php
$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[] = strtolower($file);
		}
	}
	closedir($handle);
}

echo '<p>Files</p>' ;
if ($file_array) {
	sort ($file_array);
	foreach ($file_array as $file) {
		echo "<a href='$dir/$file'>$file</a><br>";
	}
}
?>
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Okay...
So what's the problem?
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

There were no problems, I just thought I'd post my final code in case there was someone else with the similar problem they could see how I did it.
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

Well actually there is another question I have. Now that I have organized them into an array I need to insert them into my calendar. However I'm a bit stuck on how exactly to go about doing it here's what I have:

Code: Select all

include 'calendarparent.php';
$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[] = strtolower($file);
		}
	}
	closedir($handle);
}
$month = substr($file, 0,2);
$day = substr($file, 2,2);
$year = substr($file, 4,4);
if ($file_array) {
	sort ($file_array);
	foreach ($file_array as $file) {
		$days = array();
	while($month == '01'){
	$day1 = $day;
	$days[$day1] = array('$dir/$file', 'linked-day');
	}
}
I want to put each month into an array with their days. Here's the way I do it now with an event system so you can see how the calendar receives the days and links them:

Code: Select all

$cmonth = date('n');
    $events = mysql_query("SELECT *, DATE_FORMAT(date, '%M %e, %Y %h:%m:%s') as
            date,  DATE_FORMAT(date, '%c') as month,  DATE_FORMAT(date, '%e') as day FROM calevents order by date desc") or die(mysql_error());
        if($events)
          {
          $days = array();
          while($row = mysql_fetch_array($events))
            {
            if($row['month'] == $cmonth)
              {
              $day = $row['day'];
              $days[$day] = array('events.php?action=view&id='.$row['id'], 'linked-day');
              }
           }
        }


Thank you so much for the help. linked-day represents that it's a day with a link for it. $day is just the numerical representation of the day.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Maybe a multi-dimentional array would help?

Code: Select all

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, 0,2);
                                $file_array[$i]['month'] = substr($file, 2,2);
                                $file_array[$i]['year'] = substr($file, 4, 4);                           
                         }
                }
                $i ++;
You can stick

Code: Select all

<?php print "<pre>";
print_r($file_array);
print "</pre>";
at the end of the script to see what comes out, and I think you could probably take it from there.
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

That's great! I see exactly what's going on but I'm not good with arrays at all, how would I grab information from a multidimensional array and put that information like filename, day, month, and year into the format I need for my calendar which is:

Code: Select all

$days = array(
         2=>array('/weblog/archive/2004/Jan/02','linked-day'),
         3=>array('/weblog/archive/2004/Jan/03','linked-day'),
         8=>array('/weblog/archive/2004/Jan/08','linked-day'),
         22=>array('/weblog/archive/2004/Jan/22','linked-day'),
    );
I need to have a different $days array for each month, so the next one would probably be called $days2 and the number will just signify the month.
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

Any ideas anyone.
Post Reply