Page 1 of 1

Calendar / Files in a Directory

Posted: Mon May 07, 2007 9:07 pm
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);
?>

Posted: Tue May 08, 2007 6:09 am
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().

Posted: Tue May 08, 2007 7:09 am
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>";
	}
}
?>

Posted: Tue May 08, 2007 7:29 am
by Grim...
Okay...
So what's the problem?

Posted: Tue May 08, 2007 9:37 am
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.

Posted: Tue May 08, 2007 10:11 am
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.

Posted: Tue May 08, 2007 12:31 pm
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.

Posted: Wed May 09, 2007 7:57 pm
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.

Posted: Mon May 14, 2007 6:23 am
by aspekt9
Any ideas anyone.