Jump to content

[SOLVED] Loops


dylandcor

Recommended Posts

I have a code that checks if a particular word document exists.  The content gets updated most weeks and the word documents have the form LeaderMMDDYY.doc.

 

Is there any way to extend this script so that if a file doesn't exist, it will create a link to the most recent file going back week by week until it finds one?  All I would need it to change would be $last date.  I know that this script is laziness at it's finest, but it would make my life easier.

 

$last_date = "032909";  //This is what I need to change with the loop.
$sunday = date('mdy', strtotime('last sunday'));
if (file_exists("../a/b/Leader$sunday.doc")){
$leader = "<a href='http://www.domain.com/a/b/Leader$sunday.doc'>Leader Questions</a>";
}else{
$leader = "<a href='http://www.domain.com/a/b/Leader$last_date.doc'>Leader Questions</a>";
}

 

Any suggestions would be greatly appreciated.  Thanks!

Link to comment
Share on other sites

We can read in all the filenames with glob() and then find the most recent:

 

<?php
$files = glob('../a/b/Leader*.doc');
$newest = 0;
foreach ($files as $file) {
   $filename = basename($file);
   //construct standard date (yyyy-mm-dd) from filename
   $stddate = '20' . substr($filename, 10, 2) . '-' . substr($filename, 6, 2) . '-' . substr($filename, 8, 2);
   //if file's date is more recent than the most recent yet, update the most recent $newest
   if (strtotime($stddate) > $newest) {
      $newest = strtotime($stddate);
   }
}
$last_date = date('mdy', strtotime($newest));
?>

 

Your code except the first line would then follow.

Link to comment
Share on other sites

Thank you for your quick reply.

 

Your code seems to be working except one tiny thing.  It links to Leader041000.doc?

 

Any suggestions?

 

This is my current code

$files = glob('./a/b/Leader*.doc');
$newest = 0;
foreach ($files as $file) {
   $filename = basename($file);
   //construct standard date (yyyy-mm-dd) from filename
   $stddate = '20' . substr($filename, 10, 2) . '-' . substr($filename, 6, 2) . '-' . substr($filename, 8, 2);
   //if file's date is more recent than the most recent yet, update the most recent $newest
   if (strtotime($stddate) > $newest) {
      $newest = strtotime($stddate);
   }
}
$last_date = date('mdy', strtotime($newest));
$sunday = date('mdy', strtotime('last sunday'));
if (file_exists("./a/b/Leader$sunday.doc")){
$leader = "<a href='http://www.domain.com/a/b/Leader$sunday.doc'>Leader Questions</a>";
}else{
$leader = "<a href='http://www.domain.com/a/b/Leader$last_date.doc'>Leader Questions</a>";
}

Link to comment
Share on other sites

This is what came up:

Array
(
    [0] => ./documents/agendas/Leader030809.doc
    [1] => ./documents/agendas/Leader031509.doc
    [2] => ./documents/agendas/Leader032209.doc
    [3] => ./documents/agendas/Leader032909.doc
    [4] => ./documents/agendas/Leader041209.doc
)

Link to comment
Share on other sites

Ah, had a simple mistake. strtotime() shouldn't be run on $newest, since it's already a time stamp:

 

$last_date = date('mdy', $newest);

 

And if you simply want to link to the latest doc, you can shorten your code:

 

<?php
$files = glob('./a/b/Leader*.doc');
$newest = 0;
foreach ($files as $file) {
   $filename = basename($file);
   //construct standard date (yyyy-mm-dd) from filename
   $stddate = '20' . substr($filename, 10, 2) . '-' . substr($filename, 6, 2) . '-' . substr($filename, 8, 2);
   //if file's date is more recent than the most recent yet, update the most recent $newest
   if (strtotime($stddate) > $newest) {
      $newest = strtotime($stddate);
   }
}
$last_date = date('mdy', $newest);
$leader = "<a href='http://www.domain.com/a/b/Leader$last_date.doc'>Leader Questions</a>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.