Log in

View Full Version : File Sorting (Like A Highscore Board)



M2com
05-29-2012, 03:36 AM
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!

james438
05-29-2012, 04:34 AM
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)?

djr33
05-29-2012, 05:56 AM
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.

M2com
05-30-2012, 11:45 PM
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);
}
?>

djr33
05-31-2012, 03:14 AM
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.

M2com
05-31-2012, 11:35 PM
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!

djr33
06-01-2012, 03:10 AM
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.

M2com
06-02-2012, 05:54 PM
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!