Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Need help with arrays

  1. #1
    Join Date
    Mar 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with arrays

    I'm a bit of a n00b when it comes to PHP, anyway, I am in process of making a website about Gaming News, and I decided to put all of my different news stories into different .txt files on a different directory. I have everything done fine except one thing: my news pieces are shown in the alphabetical order of the .txt filenames. That means that the latest story could be on page 53 or whatever. I have tried making codes that shows the news in the order of the creation/modification date of the .txt file.

    Here is what I tried:

    PHP Code:
    <?php

    function returnfiles($dirname="docs/") {
       
    $pattern="\.(txt)$";
       
    $files = array();
       
    $fileloopcounter=0;
       if(
    $handle opendir($dirname)) 
           {
           while(
    false !== ($file readdir($handle)))
                   {
                   if(
    eregi($pattern$file)){
             
    $filedate=filemtime($dirname.$file);
                       
    $files[] = array($file$filedate);
                     
    $fileloopcounter++;
                   }
           }

           
    closedir($handle);
       }
       return(
    $files);
    }

    function 
    DateCmp($a$b)
    {
      return (
    $a[1] < $b[1]) ? -0;
    }

    function 
    SortByDate($files)
    {
      
    usort($files'DateCmp');
    }

    $files returnfiles($dirname="docs/");
    SortByDate($files);
    echo 
    '<ol>';
    foreach (
    $files as $file => $val)
    {
      echo 
    '<li>'$val'</li>';
    }
    echo 
    '</ol>';
    ?>
    And then I just get this when I open the file on a browser:



    Can someone please help me with my problem?

    Thanks in advance .

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
      echo '<li>', $val[0], '</li>';
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ah . Thank you very much . How could I put the result in reverse order?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function DateCmp($a, $b)
    {
      return !($a[1] < $b[1]) ? -1 : 0;
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Code:
    function DateCmp($a, $b)
    {
      return !($a[1] < $b[1]) ? -1 : 0;
    }
    Doesn't work... Strange...

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Really? Hm. Try:
    Code:
    function DateCmp($a, $b)
    {
      return ($a[1] > $b[1]) ? -1 : 0;
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Still the same thing...

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Paste here (in [code] tags) the output when using all the last three code blocks I posted.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The output is exactly the same as when you first fixed the code. All the files are listed in order of when they were modified, except all of the codes show the output with the newly modified files at the bottom.

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    there's probly an easier way, but you could just create a new loop that reverses after the fact... Or just start a variable and count down.
    Then again, I'm sure Twey's method is easier and will work once you figure out what the issue is.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •