Getting by with a little php help from a friend

Discuss Programming
Post Reply
byrdman
administrator
administrator
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud

Getting by with a little php help from a friend

Post by byrdman » Wed Jun 22, 2011 12:41 pm

I am wondering if someone out there can help me out. I have a php file that is supposed to search a directory for files, compare the current date and time to parts in the file, and echo out the contents of that file based on the conditions. Below is my code:

Code: Select all

<?PHP
$dir='/path/to/xml/';
$defaultfile = '/location/of/default/file.xml';
$curdate = date("Ymd");
$curtime = date("Gi");
$files = scandir($dir);
foreach ($files as $file)
{
  if ($file!='.' && $file!='..')
  {
    $array = explode("_",$file);
    $pname = $array[0];
    $dstart = $array[1];
    $tstart = $array[2];
    $dstop = $array[3];
    $tstop = $array[4];
    $tstop = rtrim($tstop, '.xml');
      if (($curdate >= $dstart)
       && ($curdate <= $dstop)
       && ($curtime >= $tstart)
       && ($curtime <= $tstop))
   {
        echo file_get_contents("$dir$file");
        break;
   }
       else
   {
       echo file_get_contents("$defaultfile");
        break;
   }
  }
}
?>
The files located in the $dir path are as follows: morning_20110620_0800_20110625_1200.xml & afternoon_20110620_1201_20110625_1800.xml. I am trying to get the above code to break apart each file into its corresponding array element. Then its to compare the start and stop date and time - based on $curdate and $curtime. It works when I don't have a 'else echo...$defaultfile' line in there. I am trying to get it so after the 25th of June, it reverts back to the default playlist. Can anyone see where I have mis-coded? Thank you for reading!

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Re: Getting by with a little php help from a friend

Post by Void Main » Thu Jun 23, 2011 3:18 pm

Man I still haven't had time to look into this so hopefully you were able to solve the issue. I have been unbelievably swamped. I still don't have time to dig into it but had time to type this. :)

byrdman
administrator
administrator
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud

Re: Getting by with a little php help from a friend

Post by byrdman » Thu Jun 23, 2011 6:46 pm

I still haven't been able to figure it out. What I think is happening is the php checks the first file and if it fails all the conditions, then it goes to the 'else' part of the code. I need it to check all the files, whether there are two files or six files, before breaking out to the 'else.' It seems like I need another 'foreach $files' but if I knew what I was doing, then I wouldn't be here! :)

byrdman
administrator
administrator
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud

Re: Getting by with a little php help from a friend

Post by byrdman » Mon Jun 27, 2011 2:18 pm

After a little advice from a coworker, My code now looks like :

Code: Select all

<?PHP
$dir="/var/www/html/mm5/xml/";
$defaultfile = "/var/www/html/mm5/Playlist.xml";
$curdate = date("Ymd");
$curtime = date("Gi");
$files = scandir($dir);
$nofile='1';
foreach ($files as $file)
{
  if ($file!='.' && $file!='..')
  {
    $array = explode("_",$file);
    $pname = $array[0];
    $dstart = $array[1];
    $tstart = $array[2];
    $dstop = $array[3];
    $tstop = $array[4];
    $tstop = rtrim($tstop, '.xml');

      if ($curdate >= $dstart && $curdate <= $dstop
      && $curtime >= $tstart && $curtime <= $tstop)
   {
    echo file_get_contents("$dir$file");
    $nofile='0';
   }
  }
}
  if ($nofile=='1')
  {
    echo file_get_contents("$defaultfile");
  }
?>
This now works. I needed the last if statement to check if the conditions were met or not.

Post Reply