Jump to content

Archived

This topic is now archived and is closed to further replies.

pgsjoe

can this be done, if so, how?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
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 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[/i]

[b]what I want:[/b]
[i]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[/i]

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

[i]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]


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]
<?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);
}
?>   
[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.