Jump to content


Photo

can this be done, if so, how?


  • Please log in to reply
9 replies to this topic

#1 pgsjoe

pgsjoe
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 08 September 2006 - 04:48 PM

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.

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 08 September 2006 - 04:50 PM

It's possible, but will be utterly complex.

#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 08 September 2006 - 05:05 PM

A quick example:

<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>

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'


Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#4 pgsjoe

pgsjoe
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 08 September 2006 - 05:55 PM

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?

#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 08 September 2006 - 06:21 PM

Array them by the YYMM and then sort.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#6 pgsjoe

pgsjoe
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 08 September 2006 - 06:52 PM

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.

#7 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 08 September 2006 - 07:07 PM

<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>

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)

)


Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#8 pgsjoe

pgsjoe
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 08 September 2006 - 08:21 PM

god, I love this forum!

#9 pgsjoe

pgsjoe
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 19 September 2006 - 09:39 PM

ran into my next problem in "splitting the array". how can I control what it's spitting out? here's what I mean.

spitting out:
September 2006 - pdf version
August 2006 - pdf version
February 2006 - pdf version
November 2005 - pdf version
October 2005 - pdf version
July 2005 - pdf version
December 2004 - pdf version


what I want:
2006 Newsletters:
September 2006 - pdf version
August 2006 - pdf version
February 2006 - pdf version

2005 Newsletters:
November 2005 - pdf version
October 2005 - pdf version
July 2005 - pdf version

2004 Newsletters:
December 2004 - pdf version


or to be able to specify a range to print out...like so....

2005-2006 Newsletters (July - June):
June 2006
April 2006
December 2005
September 2005
July 2005

2004-2005 Newsletters:
June 2005
May 2005
December 2005
July 2004



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:
<?php
if ($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);
		}
?>    


#10 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 20 September 2006 - 02:16 AM

Here's the first example. The range will be trickier.

<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>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users