Page 1 of 1

sorting after readdir

Posted: Mon Aug 30, 2010 1:18 am
by ZachsCompServ
Ok, I am kind of new to PHP, and this is my second time asking for help with php coding. I've been searching all over the internet and in my php book about this problem and can't seem to figure it out. I think arrays is the way to go about this and have attempted it with the coding below.

Here's what I have so far:

Code: Select all

<?php
if ($handle = opendir('sermons')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
         $apostrophe = str_replace("'","'",$file);
         $base = basename($file,".txt");
         $explode = explode( ", ", $base);
         echo $explode[1];
         echo "<br>";
         //   echo "<a href='sermons/$apostrophe'>$base\n</a><br>";
        }
    }
    closedir($handle);
}
?>
the "//" is in there to not execute because I was in the middle of testing something. You can see how I got this code by visiting this thread: viewtopic.php?f=1&t=120433

I'm using explode() to get the dates in the filename of my files separate so I can sort by the date. For example:

file1, 09-23-2009.txt => 09-23-2009
file2, 08-25-2007.txt => 08-25-2007
file2, 02-23-2010.txt => 02-23-2010

With the coding above, I was able to get it to just echo the date. Now, how can I turn the string into an actual, recognized date so then I could use some sort of "sort" command to sort it by date, while still echoing the full file name minus the ".txt"? Any help will be appreciated it. Thank you so much in advance for help!

Re: sorting after readdir

Posted: Mon Aug 30, 2010 1:45 am
by phpcip28
OK This is a simple task.

One way to approach this, will be to transform the dates to unix timestamps and than using the sort function to sort your dates array.

So here's how it's done:

Code: Select all

//Assign an array to help out with the sort
$output_dates = array();
if ($handle = opendir('sermons')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
         $apostrophe = str_replace("'","'",$file);
         $base = basename($file,".txt");
         $explode = explode( ", ", $base);

         //We assume this is your date in format mm-dd-yyyy
         $date = $explode[1];
         
         //Of course you could use strtotime or something but we do this the easy way here
         $date_arr = explode("-", $date);
         $day = $date_arr[1]; 
         $month = $date_arr[0]; 
         $year = $date_arr[2];

          
          //Make a unix timestamp out of each date
          $timestamp = mktime(0, 0, 0, $month, $day, $year);
          
          //Add this timestamp to the $output_dates array
         $output_dates[$file] = array();
          array_push($output_dates[$file], $timestamp);

         //   echo "<a href='sermons/$apostrophe'>$base\n</a><br>";
        }
    }
    closedir($handle);
}

//Now we do whatever we wish with the $output dates array once we're outside the loop.
//This demonstrates how we sort it from the oldest date to the newest date and pretty prints out the dates.

sort($output_dates); //This sorts in ascending order. For descending, use rsort($output_dates);

foreach($output_dates as $filename=>$date){
    $apostrophe = str_replace("'","'",$filename);
    $base = basename($filename,".txt");
    echo "<a href='sermons/$apostrophe'>$base\n</a><br>";
    echo date("m-d-Y", $date) . "<br />\n";
}
Might have maybe a few parse errors that you'll need to check for but otherwise this should work out of the box with what you have there.

Hope it helps.

Re: sorting after readdir

Posted: Mon Aug 30, 2010 2:28 am
by ZachsCompServ
This is the output I got:

[text]
0

Warning: date() expects parameter 2 to be long, array given in C:\xampp\htdocs\sjucc\sermons2.php on line 77

1

Warning: date() expects parameter 2 to be long, array given in C:\xampp\htdocs\sjucc\sermons2.php on line 77

2

Warning: date() expects parameter 2 to be long, array given in C:\xampp\htdocs\sjucc\sermons2.php on line 77

3

Warning: date() expects parameter 2 to be long, array given in C:\xampp\htdocs\sjucc\sermons2.php on line 77
[/text]

Each of the numbers (0 through 3) were linked to "/sermons/#" where "#" equals the number listed (i.e. "/sermons/0")

I'm gonna put a hold on this for tonight and head to bed. Thanks for your attempted help though. I look forward to more help from you and others within the next day or two. Thanks again!

Re: sorting after readdir

Posted: Mon Aug 30, 2010 2:37 am
by phpcip28
OK small glitch... sorry about that
Try this.

Tested and works.

Code: Select all

<?php
//Assign an array to help out with the sort
$output_dates = array();
if ($handle = opendir('sermons')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
         $apostrophe = str_replace("'","'",$file);
         $base = basename($file,".txt");
         $explode = explode( ", ", $base);

         //We assume this is your date in format mm-dd-yyyy
         $date = $explode[1];
         
         //Of course you could use strtotime or something but we do this the easy way here
         $date_arr = explode("-", $date);
         $day = $date_arr[1]; 
         $month = $date_arr[0]; 
         $year = $date_arr[2];

          
          //Make a unix timestamp out of each date
          $timestamp = mktime(0, 0, 0, $month, $day, $year);

          //Add this timestamp to the $output_dates array
          $output_dates[$file] = $timestamp;

        }
    }
    closedir($handle);
}

//Now we do whatever we wish with the $output dates array once we're outside the loop.
//This demonstrates how we sort it from the oldest date to the newest date and pretty prints out the dates.

sort($output_dates); //This sorts in ascending order. For descending, use rsort($output_dates);

foreach($output_dates as $filename=>$date){
    $apostrophe = str_replace("'","'",$filename);
    $base = basename($filename,".txt");
    echo "<a href='sermons/$apostrophe'>$base\n</a> - ";
    echo date("m-d-Y", $date) . "<br />\n";
}
?>

Re: sorting after readdir

Posted: Mon Aug 30, 2010 12:24 pm
by ZachsCompServ
Gave that a try, was still getting some error messages. I tried messing around with it as you said you didn't test it, and just couldn't figure anything out. I'm lost now haha. Thanks for trying, any other suggestions, or any suggestions from anyone else? Thanks

Re: sorting after readdir

Posted: Mon Aug 30, 2010 12:33 pm
by phpcip28
ZachsCompServ wrote:Gave that a try, was still getting some error messages. I tried messing around with it as you said you didn't test it, and just couldn't figure anything out. I'm lost now haha. Thanks for trying, any other suggestions, or any suggestions from anyone else? Thanks
That's strange I tested it myself. What error did you get

By the way.. don't know when you tried it, I made small mods to it bout a couple hours ago... You might wanna try again as it is in there.

Re: sorting after readdir

Posted: Mon Aug 30, 2010 12:37 pm
by AbraCadaver
ZachsCompServ wrote:Gave that a try, was still getting some error messages. I tried messing around with it as you said you didn't test it, and just couldn't figure anything out. I'm lost now haha. Thanks for trying, any other suggestions, or any suggestions from anyone else? Thanks
I prefer glob():

Code: Select all

foreach(glob('sermons/*.txt') as $file) {
	list($name, $date) = explode( ", ", basename($file, ".txt"));
	$files[$name] = strtotime($date);
}
sort($files);

foreach($files as $name => $date) {
	echo "$name and $date";
}

Re: sorting after readdir

Posted: Mon Aug 30, 2010 12:49 pm
by ZachsCompServ
phpcip28 wrote:
ZachsCompServ wrote:Gave that a try, was still getting some error messages. I tried messing around with it as you said you didn't test it, and just couldn't figure anything out. I'm lost now haha. Thanks for trying, any other suggestions, or any suggestions from anyone else? Thanks
That's strange I tested it myself. What error did you get

By the way.. don't know when you tried it, I made small mods to it bout a couple hours ago... You might wanna try again as it is in there.
Just tried it again as you said you made small mods to it recently. When I did, the dates were sorted, but the links don't work.

Section of page looks like this:

0 - 02-25-2007
1 - 09-14-2008
2 - 09-21-2008
3 - 09-28-2008

the numbers are linked to "/sermons/x" where x = the numbers 0 thru 3. I'm taking another break on this, I'll get back to you later today or tomorrow. Thanks for your help though

Re: sorting after readdir

Posted: Mon Aug 30, 2010 1:20 pm
by phpcip28
OK... please try this

And... for your own good... in your future endeavors with PHP, PLEASE make a slight effort to understand what the code does...

Code: Select all

<?php
//Assign an array to help out with the sort
$output_dates = array();
$output_files = array();

//Setup the directory to open here.
$directory_to_open = 'sermons';

if ($handle = opendir($directory_to_open)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
         $apostrophe = str_replace("'","'",$file);
         $base = basename($file,".txt");
         $explode = explode( ", ", $base);

         //We assume this is your date in format mm-dd-yyyy
         $date = $explode[1];
         
         //Of course you could use strtotime or something but we do this the easy way here
         $date_arr = explode("-", $date);
         $day = $date_arr[1]; 
         $month = $date_arr[0]; 
         $year = $date_arr[2];

          
          //Make a unix timestamp out of each date
          $timestamp = mktime(0, 0, 0, $month, $day, $year);

          //Add this timestamp to the $output_dates array
          $output_dates[] = $timestamp;
          $output_files[$timestamp] = $directory_to_open . "/" . $file;

        }
    }
    closedir($handle);
}


//Now we do whatever we wish with the $output dates array once we're outside the loop.
//This demonstrates how we sort it from the oldest date to the newest date and pretty prints out the dates.

sort($output_dates); //This sorts in ascending order. For descending, use rsort($output_dates);

foreach($output_dates as $date){
    $file_link = $output_files[$date];
    $base = basename($output_files[$date],".txt");
    
    //Echo out the links. NOTE that you don't need the directory name here anymore.
    echo "<a href='{$file_link}'>{$base}</a> - " . date("m-d-Y", $date) . "<br />\n";
}
?>

Re: sorting after readdir

Posted: Mon Aug 30, 2010 2:05 pm
by AbraCadaver
Last post. This should work:

Code: Select all

foreach(glob('sermons/*.txt') as $file) {
        list($name, $date) = explode( ", ", $file);
        $files[$name] = strtotime($date);
}
sort($files);

foreach($files as $name => $date) {
        $text = basename($name, ".txt");
        $link = htmlentities($name);
        echo "<a href='sermons/$link'>$text</a><br>";
}

Re: sorting after readdir

Posted: Mon Aug 30, 2010 2:52 pm
by ZachsCompServ
phpcip28 wrote:OK... please try this

And... for your own good... in your future endeavors with PHP, PLEASE make a slight effort to understand what the code does...

Code: Select all

<?php
//Assign an array to help out with the sort
$output_dates = array();
$output_files = array();

//Setup the directory to open here.
$directory_to_open = 'sermons';

if ($handle = opendir($directory_to_open)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
         $apostrophe = str_replace("'","'",$file);
         $base = basename($file,".txt");
         $explode = explode( ", ", $base);

         //We assume this is your date in format mm-dd-yyyy
         $date = $explode[1];
         
         //Of course you could use strtotime or something but we do this the easy way here
         $date_arr = explode("-", $date);
         $day = $date_arr[1]; 
         $month = $date_arr[0]; 
         $year = $date_arr[2];

          
          //Make a unix timestamp out of each date
          $timestamp = mktime(0, 0, 0, $month, $day, $year);

          //Add this timestamp to the $output_dates array
          $output_dates[] = $timestamp;
          $output_files[$timestamp] = $directory_to_open . "/" . $file;

        }
    }
    closedir($handle);
}


//Now we do whatever we wish with the $output dates array once we're outside the loop.
//This demonstrates how we sort it from the oldest date to the newest date and pretty prints out the dates.

sort($output_dates); //This sorts in ascending order. For descending, use rsort($output_dates);

foreach($output_dates as $date){
    $file_link = $output_files[$date];
    $base = basename($output_files[$date],".txt");
    
    //Echo out the links. NOTE that you don't need the directory name here anymore.
    echo "<a href='{$file_link}'>{$base}</a> - " . date("m-d-Y", $date) . "<br />\n";
}
?>
Ok, it works! Thank you very much!

Yes, I know I need to learn php more. I am a newbie to it, I'm reading books and using online resources. I have a very technical mind with programming, and can figure things out when I look at things. Like, I'm going to take the time one of these days to actually read the programming you did for me and try to learn what it means by referencing commands to my book or to online resources. This is really the only thing that I needed advanced php programming for as far as this website is concerned. I am learning php on my own time and slowly. We all start somewhere, right?

Thanks again for your help though. I do appreciate everything!!!

Re: sorting after readdir

Posted: Mon Aug 30, 2010 2:54 pm
by ZachsCompServ
AbraCadaver wrote:Last post. This should work:

Code: Select all

foreach(glob('sermons/*.txt') as $file) {
        list($name, $date) = explode( ", ", $file);
        $files[$name] = strtotime($date);
}
sort($files);

foreach($files as $name => $date) {
        $text = basename($name, ".txt");
        $link = htmlentities($name);
        echo "<a href='sermons/$link'>$text</a><br>";
}
AbraCadaver, I thank you for your time and effort. I'm going to go with the other programming just because that works and that person spent the last 10 hours or so helping me with this. I apologize if you feel that your time was wasted, don't worry though. I will test it out eventually. Thanks again!

Re: sorting after readdir

Posted: Mon Aug 30, 2010 3:01 pm
by phpcip28
I totally agree.
We were all once newbies at this.

The whole point I guess is to understand programming in general not php specific. Of course, knowing php specifics and functions will help a lot, but the key is finding the algorithm that works.
How you choose to implement the algorithm, is simply a matter of choice and a matter of taste in languages.

I, Myself, am rather experienced with php but still working on with python projects as well.

Wish you best of luck with everything.
Oh and take it easy ;)

Re: sorting after readdir

Posted: Mon Aug 30, 2010 3:02 pm
by ZachsCompServ
phpcip28 wrote:OK... please try this

And... for your own good... in your future endeavors with PHP, PLEASE make a slight effort to understand what the code does...

Code: Select all

<?php
//Assign an array to help out with the sort
$output_dates = array();
$output_files = array();

//Setup the directory to open here.
$directory_to_open = 'sermons';

if ($handle = opendir($directory_to_open)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
         $apostrophe = str_replace("'","'",$file);
         $base = basename($file,".txt");
         $explode = explode( ", ", $base);

         //We assume this is your date in format mm-dd-yyyy
         $date = $explode[1];
         
         //Of course you could use strtotime or something but we do this the easy way here
         $date_arr = explode("-", $date);
         $day = $date_arr[1]; 
         $month = $date_arr[0]; 
         $year = $date_arr[2];

          
          //Make a unix timestamp out of each date
          $timestamp = mktime(0, 0, 0, $month, $day, $year);

          //Add this timestamp to the $output_dates array
          $output_dates[] = $timestamp;
          $output_files[$timestamp] = $directory_to_open . "/" . $file;

        }
    }
    closedir($handle);
}


//Now we do whatever we wish with the $output dates array once we're outside the loop.
//This demonstrates how we sort it from the oldest date to the newest date and pretty prints out the dates.

sort($output_dates); //This sorts in ascending order. For descending, use rsort($output_dates);

foreach($output_dates as $date){
    $file_link = $output_files[$date];
    $base = basename($output_files[$date],".txt");
    
    //Echo out the links. NOTE that you don't need the directory name here anymore.
    echo "<a href='{$file_link}'>{$base}</a> - " . date("m-d-Y", $date) . "<br />\n";
}
?>
Made one little change. Where you had (after the sort command):

Code: Select all

$file_link = $output_files[$date];
I changed it to:

Code: Select all

$apostrophe = str_replace("'","'",basename($output_files[$date]));
and then put in $apostrophe where you had $file_link at the end of the programming. That takes care of any file names with an apostrophe in it.

Everything works as needed now! Thanks again!

Re: sorting after readdir

Posted: Mon Aug 30, 2010 3:03 pm
by ZachsCompServ
phpcip28 wrote:I totally agree.
We were all once newbies at this.

The whole point I guess is to understand programming in general not php specific. Of course, knowing php specifics and functions will help a lot, but the key is finding the algorithm that works.
How you choose to implement the algorithm, is simply a matter of choice and a matter of taste in languages.

I, Myself, am rather experienced with php but still working on with python projects as well.

Wish you best of luck with everything.
Oh and take it easy ;)
Thanks a bunch! Wish you the best of luck with everything as well. Good luck with the python projects! You take it easy too! :D