Results 1 to 8 of 8

Thread: File Sorting (Like A Highscore Board)

  1. #1
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Exclamation File Sorting (Like A Highscore Board)

    I've been working on this for a while but I can't wrap my head around doing this and making it work! I'm trying to get PHP to all the text files in a directory and based on the contents it would sort all of the text files from greatest to least sort of like a High Score Board.

    Thanks!

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,349
    Thanks
    99
    Thanked 112 Times in 110 Posts

    Default

    What do you mean by "greatest" to "least"? Can you give an example? Are you talking about file names or the values inside the text files? Are the values alphabetical or numerical (I'm gonna guess the latter here)?
    To choose the lesser of two evils is still to choose evil. My personal site

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

    Default

    This is completely possible, and also will be very slow. Why do you want to do this? I'd guess there's a better way. Using filenames is more reasonable, but using file contents will just take a lot of system resources (memory and processing) and be extremely inefficient unless the contents are small, in which case you might want to use a database instead. In fact, whatever you're doing, using a database is probably a better idea, even if it contains references to real files (mirror the data in the database, like which files are "biggest", then link to a real file, or whatever else you want to do with the file data-- similar to how image databases work-- store the images as files and the information about them in the DB).

    The better way to approach this would probably be command line to go through the files faster, then get the filenames that way.

    If you want to do this, it's actually not very difficult (just, probably, a bad idea as I explained above):
    1. Get an array of all files in the directory. There are a few ways, but one is to use glob() to get an array of all files, and maybe sure to eliminate the . (self) and .. (higher level) directory references. (I'm actually not sure those always show up with glob(), but they may. They do with other PHP methods.)
    2. Use a foreach() loop to go through each of them. [Alternatively skip the glob() step and use opendir() and readdir() to loop through each file-- your choice.]
    3. For each file, use file_get_contents() to figure out what is inside it (based on whatever method you want), then store this information in an array relative to the filename.
    4. Sort the array of info/filenames by whatever criteria you have. Use usort() if necessary. (Here I would strongly recommend abstracting away from the files-- at this point you should have numbers or something, not full text documents, unless you really really need them for something like natural language processing.)
    5. Your new array should now be in the right order, so display it however you'd like.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default

    Thanks! I got the opening up the directory and everything but the array and sorting part is what I'm having a problem with, I really don't know how to that. If you could help set me straight that would be great! Here's my code:

    <?php

    if ($handle = opendir('glikes/')) {
    echo "Directory handle: $handle\n";
    echo "Entries:\n";




    while (false !== ($entry = readdir($handle))) {



    $contents = file_get_contents('glikes/'.$entry);

    $array = array($entry => $contents);
    $dis=usort($array, "cmp");
    echo "$dis";



    }



    closedir($handle);
    }
    ?>

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

    Default

    Your usort() function "cmp" is not defined. Look at the usort page at php.net for more information about how to define it. I have no idea how you want to compare the data, so I can't help, at least not with more information. Look at the example there for comparing greater than and less than for numbers, and then just base yours on that. For example, if it is filesize, just see which file contains more data, and use that as "greater than", etc.

    This line doesn't make any sense to me:
    $array = array($entry => $contents);

    I think what you mean is:
    $array[$entry] = $contents;

    Your version will replace the entire array with a new array containing just one entry. My suggested version will add a new entry based on $entry with the data for the file.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default

    Okay thanks very much! Now I just ventured into the cmp thing:

    <?php
    function cmp($array, $b)
    {
    if ($array == $b) {
    return 0;
    }
    return ($array < $b) ? -1 : 1;
    }

    if ($handle = opendir('glikes/')) {
    echo "Directory handle: $handle\n";
    echo "Entries:\n";




    while (false !== ($entry = readdir($handle))) {




    $contents = file_get_contents('glikes/'.$entry);


    $array[$entry] = $contents;

    $dis=usort($array, "cmp");
    echo "$dis";



    }



    closedir($handle);
    }
    ?>

    The cmp code in is probably not the one I will or should use but I just needed an example of how to do it, but I'm totally lost and also when I run the code I get a bunch of 1's. Is that the file contents or the actually commands, because I know the files have 1's but they also have 2's as well.

    Basically what I want it to do is read the "glikes/" text file contents and another folder with the same file name and get its contents "postcontents/". Then sort all the files based on the number in the "glikes/" file and display the contents with it. The usort needs to be greatest to least.

    Thanks so much for your help and let me know if you need further clarification!
    Last edited by M2com; 05-31-2012 at 11:49 PM.

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

    Default

    I still don't understand the logic of your sort function. What exactly is your goal with it? If you're doing a purely numerical operation (and can find the right indices) then you could use sort() or ksort() alone. But if you need something more, you can use usort() with your own function. If you can write out step by step how to compare the information, it will be easy to write the cmp function. You should look at more examples to see how it works.
    Or just explain exactly you how want to compare it. One good option is to give some example data.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default

    Thanks for your help! Some data from a few of the files: 0,1,2,3 (that's basically what the files will look like they're text files by the way). Sorting:

    The data will be sorted by greatest to least so:
    3
    2
    1
    0
    To do this, the script will read all the files in the directory and sort all the info it read into greatest to least. When it's sorting it will also read another file with the same name but in a different directory and display its contents with the number.

    Hope this clarifies some more!

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
  •