I have the PHP script included below. It makes a calendar with links based on files present in a folder named forecasts. It looks for files formatted: bYYMMDD.php. Anyway, I need the script to check for files with the current naming convention, but in multiple folders. I don't know shit about PHP. I know enough about fortran to at least realize what this script does. If someone could tell me what I need to change to make the script browse multiple folder, I'd be greatful. For the more surly TWWers among us, if you could at least point me to a good PHP reference online that would give me a clue as to how to change the script, that would make me happy too.<?php//***********************************// MAKING THE CALENDAR //***********************************function make_calendar() { $validm = isset($_GET['month']) && preg_match('/^(?:0?[1-9]|1[0-2])$/', $_GET['month']); $validy = isset($_GET['year']) && preg_match('/\d{1,2}/', $_GET['year']); if ($validm && $validy) { $thismonth = mktime(0,0,0,$_GET["month"],1,$_GET["year"]); } else { $thismonth = mktime(0,0,0,date("m"),1,date("y")); } // $first day finds the day of the week that the first day of the month falls on $firstday = date("w",$thismonth); // $monthv = Full name of the month $monthv = date("F",$thismonth); // $yearv = year in 2-digits $yearv = date("y",$thismonth); // month in numerical format $monthc = date("m",$thismonth); // month before = current month - 1 $mb = $monthc - 1; // month after = current month + 1 $ma = $monthc + 1; // set year after $ya = $yearv; // set year before $yb = $yearv; // if month before is less than 1 set it to 12 and subtract 1 from year before if ($mb < 1){ $mb = 12; $yb = $yearv - 1; } // if month after is greater than 12 set it to 1 and add 1 to year after if ($ma > 12){ $ma = 1; $ya = $yearv + 1; } // print headers of the table and appropriate links taking the calendar backwards and forwards echo ("<tr><th><a href=\"?month=$mb&year=$yb\"><</a></th><th colspan=\"5\">$monthv $yearv</th><th><a href=\"?month=$ma&year=$ya\">></a></th></tr><tr class=\"trx\"><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr><tr>"); // print empty table cells for the beginning of the months for($c=0; $c < $firstday; $c++){ echo("<td></td>"); } // print calendar $dir = "forecasts"; $dh = opendir($dir); while (false !== ($filename = readdir($dh))) { $files[] = $filename; } rsort($files); $st = "b"; $z = "0"; $ext = ".php"; for ($k=1; $k <= date("t",$thismonth); $k++){ if ($c % 7 == 0){ echo ("</tr><tr>");} if ($k < 10 ){ $curr = $st.$yearv.$monthc.$z.$k.$ext; } else { $curr = $st.$yearv.$monthc.$k.$ext; } if (($key = array_search($curr, $files)) != NULL) { echo("<td><a href=\"forecasts/$curr?month=$monthc&year=$yearv\">$k</a></td>"); } else { echo("<td>$k</td>"); } $c++; } echo ("</tr>");}?>
2/1/2006 12:32:05 PM
replace:
// print calendar$dir = "forecasts";$dh = opendir($dir);while (false !== ($filename = readdir($dh))) { $files[] = $filename; }
// print calendar$list_of_folders = array('forecasts','another_folder','yet_another_folder');for($z=0;$z<count($list_of_folders);$z++) { $dh = opendir($list_of_folders[$z]); while (false !== ($filename = readdir($dh))) { $files[] = $filename; } }
2/2/2006 4:03:15 AM
you can do it recursively, but you are going to have to change more than just the file listing part of the code to retain the directories those files are in and then to compare dates to those values.i'll write some actual code later today when i have some time.[Edited on February 2, 2006 at 9:21 AM. Reason : .]
2/2/2006 9:19:53 AM
You guys are great. I'll test the code later today and let you know what I encounter.
2/2/2006 11:18:30 AM
ok, this function will recursively parse a directory tree and return all filenames and the directories they are under:
function parsedir($dir = ".") { $return_dir=array(); $dh = opendir($dir); while (false !== ($filename = readdir($dh))) { if(is_dir("$dir"."/"."$filename") && $filename!="." && $filename!="..") { $next_dir = parsedir("$dir"."/"."$filename"); $return_dir = array_merge($return_dir,$next_dir); } else if($filename!="." && $filename!="..") { array_push($return_dir, array("directory"=>$dir,"filename"=>$filename)); } } return $return_dir;}
$files=parsedir("forcasts");
Array( [0] => Array ( [directory] => ./dir1/subdir1 [filename] => filesubdir1 ) [1] => Array ( [directory] => ./dir1 [filename] => filedir1 ) [2] => Array ( [directory] => ./dir2/subdir2 [filename] => filesubdir2 ) [3] => Array ( [directory] => ./dir2 [filename] => filedir2 ) [4] => Array ( [directory] => . [filename] => rootfile1 ) [5] => Array ( [directory] => . [filename] => rootfile2 ) [6] => Array ( [directory] => . [filename] => index.php ))
if (($key = array_search($curr, $files)) != NULL) {
2/2/2006 3:40:27 PM
^^^^ hempster, I copied your code and it works. However, it's generating the wrong links in the calandar. The link generating part of the code is still assuming that all the files are going to come from one folder. Would the fix be as simple as changing 'forecast' to $list_of_folders in the last section of the script or do I need to something a more complex?^ I'll look at your code, it might be a more flexible solution. I'll have to take a close look at it and see how it works. I'll let you know.Thanks again to the two of you who have given me solutions. It's been a big help.[Edited on February 2, 2006 at 3:59 PM. Reason : ]
2/2/2006 3:58:41 PM
bttt...for now
2/3/2006 12:46:38 AM
Can anyone give me a clue as to how to make the files in the other folders link correctly?
2/3/2006 5:16:39 PM
you would have to keep track of which folder they are in so that you can put that in the link.
2/3/2006 7:20:51 PM
Oh shit, I didn't see that....OK,replace this:
// print calendar$list_of_folders = array('forecasts','another_folder','yet_another_folder');for($z=0;$z<count($list_of_folders);$z++) { $dh = opendir($list_of_folders[$z]); while (false !== ($filename = readdir($dh))) { $files[] = $filename; } }rsort($files);$st = "b";$z = "0";$ext = ".php";for ($k=1; $k <= date("t",$thismonth); $k++){ if ($c % 7 == 0) { echo ("</tr><tr>"); } if ($k < 10 ) { $curr = $st.$yearv.$monthc.$z.$k.$ext; } else { $curr = $st.$yearv.$monthc.$k.$ext; } if (($key = array_search($curr, $files)) != NULL) { echo("<td><a href=\"forecasts/$curr?month=$monthc&year=$yearv\">$k</a></td>"); } else { echo("<td>$k</td>"); } $c++; }
// print calendar$list_of_folders = array('forecasts','another_folder','yet_another_folder');for($z=0;$z<count($list_of_folders);$z++) { $dh = opendir($list_of_folders[$z]); while (false !== ($filename = readdir($dh))) { $files["$filename"] = $list_of_folders[$z]; } }// rsort($files);$st = "b";$z = "0";$ext = ".php";for ($k=1; $k <= date("t",$thismonth); $k++){ if ($c % 7 == 0) { echo ("</tr><tr>"); } if ($k < 10 ) { $curr = $st.$yearv.$monthc.$z.$k.$ext; } else { $curr = $st.$yearv.$monthc.$k.$ext; } $flag = "not found"; foreach($files as $the_filename=>$the_foldername) { if($the_filename == $curr) { echo("<td><a href=\"$the_foldername/$curr?month=$monthc&year=$yearv\">$k</a></td>"); $flag = "found"; } } if($flag == "not found") { echo("<td>$k</td>"); } $c++; }
2/4/2006 12:31:42 PM
You guys are great. Thanks a bunch.
2/4/2006 6:42:33 PM
FYI, I played with the code and I ran into some problems. However, I got it to work like I wanted in teh end. Below is the code I used. I didn't do it the easiest way, but at least I understand this way and was able to avoid functions that I don't fully understand how to work. You'll notice that's it's very close in operation to the original code. Thanks again for the help.// print calendar $dir1 = "forecasts"; $dir2 = "fcst2005/may05"; $dh1 = opendir($dir1); $dh2 = opendir($dir2); while (false !== ($filename1 = readdir($dh1))) { $files1[] = $filename1; } while (false !== ($filename2 = readdir($dh2))) { $files2[] = $filename2; } rsort($files1); rsort($files2); $st = "b"; $z = "0"; $ext = ".php"; for ($k=1; $k <= date("t",$thismonth); $k++){ if ($c % 7 == 0){ echo ("</tr><tr>");} if ($k < 10 ) { $curr = $st.$yearv.$monthc.$z.$k.$ext; } else { $curr = $st.$yearv.$monthc.$k.$ext; } if (($key1 = array_search($curr, $files1)) != NULL) { echo("<td><a href=\"$dir1/$curr?month=$monthc&year=$yearv\">$k</a></td>"); } else { if (($key2 = array_search($curr, $files2)) != NULL) { echo("<td><a href=\"$dir2/$curr?month=$monthc&year=$yearv\">$k</a></td>"); } else { echo("<td>$k</td>"); } } $c++; } echo ("</tr>");}
2/6/2006 5:19:20 PM
i'm glad you got it to workbuti would suggest you learn a little more complex programming. i'm not saying you have to use what i gave you, but you should use something like it. a recursive function will traverse any directory structure you make. if you make your code work off what it finds, rather than what you tell it is there, then it will always work for any change in directory structure that you make.the current way, anytime you add another directory, you are going to have to add and/or modify code.
2/6/2006 7:04:55 PM
^ I'm well aware of the fact that I'll have to edit the code pending any changes. I'll also admit that I've very new to writing PHP. I know that a recursive approach would be best, but until I learn more, I have to stick with what I know. I've been studying your code. As soon as I can write PHP well enough to implement such a solution, I fully plan to. Another aspect of what I need this code to do is to also power my linking calendar when browsing from within folders below the top level. At least with what I wrote, I understand how to make that function work as well. I tried to use hempster's solution, but for some reason, it would generate spurious links for every date in the calendar.
2/6/2006 9:25:32 PM