pgsjoe Posted September 8, 2006 Share Posted September 8, 2006 I'm still quite new to PHP so I appreciate the patience.WHAT I'VE GOT:About 75 different websites who all send me their newsletters to put up online. I have a naming convention, so each one of them gets named two digit year, then month (ex: 0609.pdf). Have to update the page manually everytime a newsletter is added as well as upload the pdf file. Can't automate by date because they all send in at different times.WHAT I'D LIKE:To have PHP read the directory and if 0609.pdf exists to echo 'September 2006 Newsletter (pdf version)' as a link.PROBLEM:Sometimes it's a PDF, sometimes it's a word DOC. Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 8, 2006 Share Posted September 8, 2006 It's possible, but will be utterly complex. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 8, 2006 Share Posted September 8, 2006 A quick example:[code]<pre><?php $tests = array( '0609.pdf', '0608.pdf', '0507.doc', 'invalid', '0000.pdf', 'abc.doc', 'file.exe', '0609.txt', ); $months = array( '01' => 'January', '02' => 'February', '03' => 'March', '04' => 'April', '05' => 'May', '06' => 'June', '07' => 'July', '08' => 'August', '09' => 'September', '10' => 'October', '11' => 'November', '12' => 'December', ); foreach ($tests as $test) { ### Make sure the name is valid. if (!preg_match('/^(\d{2})(\d{2})\.(doc|pdf)$/', $test, $matches)) { echo "Skipping invalid file '$test'<br/>"; continue; } ### Make sure the month is valid. $month = $matches[2]; if (!array_key_exists($month, $months)) { echo "Skipping invalid month for '$test'</br>"; continue; } ### Print. $year = 2000 + $matches[1]; $extension = $matches[3]; echo $months[$month], ' ', $year, ' Newsletter (', $extension, ' version)<br/>'; }?></pre>[/code][quote]September 2006 Newsletter (pdf version)August 2006 Newsletter (pdf version)July 2005 Newsletter (doc version)Skipping invalid file 'invalid'Skipping invalid month for '0000.pdf'Skipping invalid file 'abc.doc'Skipping invalid file 'file.exe'Skipping invalid file '0609.txt'[/quote] Quote Link to comment Share on other sites More sharing options...
pgsjoe Posted September 8, 2006 Author Share Posted September 8, 2006 Wow...what a beauty! Actually got it working and everything to read the directory and spit 'em out. Now I'll push my luck one further...is there a way to arrange them in reverse order so the most recent month spits out on top? Quote Link to comment Share on other sites More sharing options...
effigy Posted September 8, 2006 Share Posted September 8, 2006 Array them by the YYMM and then sort. Quote Link to comment Share on other sites More sharing options...
pgsjoe Posted September 8, 2006 Author Share Posted September 8, 2006 if you've got any "answering juice" left in you...I don't know how to do that. the good news though, I will only ever ask once and also dissect it to understand it. thanks again. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 8, 2006 Share Posted September 8, 2006 [code]<pre><?php $tests = array( '0601.doc', '0202.pdf', '0609.pdf', '0608.pdf', '0001.doc', '0507.doc', 'invalid', '0000.pdf', 'abc.doc', 'file.exe', '0609.txt', ); $months = array( '01' => 'January', '02' => 'February', '03' => 'March', '04' => 'April', '05' => 'May', '06' => 'June', '07' => 'July', '08' => 'August', '09' => 'September', '10' => 'October', '11' => 'November', '12' => 'December', ); $files = array(); foreach ($tests as $test) { ### Make sure the name is valid. if (!preg_match('/^(\d{2})(\d{2})\.(doc|pdf)$/', $test, $matches)) { echo "Skipping invalid file '$test'<br/>"; continue; } ### Make sure the month is valid. $month = $matches[2]; if (!array_key_exists($month, $months)) { echo "Skipping invalid month for '$test'</br>"; continue; } ### Print. $year = $matches[1]; $extension = $matches[3]; $files["$year$month"] = $months[$month] . ' ' . ($year + 2000) . ' Newsletter (' . $extension . ' version)<br/>'; } krsort($files, SORT_NUMERIC); print_r($files);?></pre>[/code][quote]Skipping invalid file 'invalid'Skipping invalid month for '0000.pdf'Skipping invalid file 'abc.doc'Skipping invalid file 'file.exe'Skipping invalid file '0609.txt'Array( [0609] => September 2006 Newsletter (pdf version) [0608] => August 2006 Newsletter (pdf version) [0601] => January 2006 Newsletter (doc version) [0507] => July 2005 Newsletter (doc version) [0202] => February 2002 Newsletter (pdf version) [0001] => January 2000 Newsletter (doc version))[/quote] Quote Link to comment Share on other sites More sharing options...
pgsjoe Posted September 8, 2006 Author Share Posted September 8, 2006 god, I love this forum! Quote Link to comment Share on other sites More sharing options...
pgsjoe Posted September 19, 2006 Author Share Posted September 19, 2006 ran into my next problem in "splitting the array". how can I control what it's spitting out? here's what I mean.[b]spitting out:[/b][i]September 2006 - pdf versionAugust 2006 - pdf versionFebruary 2006 - pdf versionNovember 2005 - pdf versionOctober 2005 - pdf versionJuly 2005 - pdf versionDecember 2004 - pdf version[/i][b]what I want:[/b][i]2006 Newsletters:September 2006 - pdf versionAugust 2006 - pdf versionFebruary 2006 - pdf version2005 Newsletters:November 2005 - pdf versionOctober 2005 - pdf versionJuly 2005 - pdf version2004 Newsletters:December 2004 - pdf version[/i]or to be able to specify a range to print out...like so....[i]2005-2006 Newsletters (July - June):June 2006April 2006December 2005September 2005July 20052004-2005 Newsletters:June 2005May 2005December 2005July 2004[/i]I guess what I'm asking is that I don't know how to split up the krsort that it's spitting out. Help....again?Here's the code I'm using:[code]<?phpif ($handle = opendir('./docs')) { //echo "Directory handle: $handle\n"; //echo "Files:\n"; /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { $tests = array($file); $months = array( '01' => 'January', '02' => 'February', '03' => 'March', '04' => 'April', '05' => 'May', '06' => 'June', '07' => 'July', '08' => 'August', '09' => 'September', '10' => 'October', '11' => 'November', '12' => 'December', ); foreach ($tests as $test) { ### Make sure the name is valid. if (!preg_match('/^(\d{2})(\d{2})\.(doc|pdf)$/', $test, $matches)) { //echo "Skipping invalid file '$test'<br/>"; continue; } ### Make sure the month is valid. $month = $matches[2]; if (!array_key_exists($month, $months)) { //echo "Skipping invalid month for '$test'</br>"; continue; } ### Print. $year = 2000 + $matches[1]; $extension = $matches[3]; $files["$year$month"] = $months[$month] . ' ' . $year . ' - <a href="/docs/' . $file . '" target="_blank">' . $extension . ' version</a><br/>'; } } if ($files==0) echo "no newsletters currently on file"; else { krsort($files, SORT_NUMERIC); foreach ($files as $newsletter) { echo "$newsletter"; } } closedir($handle); }?> [/code] Quote Link to comment Share on other sites More sharing options...
effigy Posted September 20, 2006 Share Posted September 20, 2006 Here's the first example. The range will be trickier.[code]<pre><?php $tests = array( '0609.pdf', '0608.pdf', '0602.pdf', '0511.pdf', '0510.pdf', '0507.pdf', '0412.pdf', ); $months = array( '01' => 'January', '02' => 'February', '03' => 'March', '04' => 'April', '05' => 'May', '06' => 'June', '07' => 'July', '08' => 'August', '09' => 'September', '10' => 'October', '11' => 'November', '12' => 'December', ); $files = array(); foreach ($tests as $test) { ### Make sure the name is valid. if (!preg_match('/^(\d{2})(\d{2})\.(doc|pdf)$/', $test, $matches)) { echo "Skipping invalid file '$test'<br/>"; continue; } ### Make sure the month is valid. $month = $matches[2]; if (!array_key_exists($month, $months)) { echo "Skipping invalid month for '$test'</br>"; continue; } ### Print. $year = $matches[1]; $extension = $matches[3]; $files["$year$month"] = $months[$month] . ' ' . ($year + 2000) . ' - ' . $extension . ' version'; } krsort($files, SORT_NUMERIC); $previous_year = ''; foreach ($files as $YYMM => $file) { ### Handle year. $current_year = substr($YYMM, 0, 2); if ($current_year != $previous_year) { echo '<br/>', ($current_year + 2000), ' Newsletters<br/>'; } $previous_year = $current_year; ### Show file. echo $file, '<br/>'; }?></pre>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.