Jump to content

Entire PHP System on Flat Files.


BxK

Recommended Posts

I've written an entire system on txt files for posting content, Im trying to take two text files:

 

 

THE POST | THE NUMBER OF LIKES
       |                               |

       '''''''''''''''|'''''''''''''''''

                   TOP

 

both are in text files. I can get that data into an array as such

 

Array

(
[../uploads/likes/55b808022bb92.txt] => 4
[../uploads/likes/55b80982e5573.txt] => 2
[../uploads/likes/55b80d627a030.txt] => 2
[../uploads/likes/55b7e6f02c8b4.txt] => 1
[../uploads/likes/55b815631be8d.txt] => 1
)

 

and I'm using 

function top()
{

if(isset($_GET['top']))
{

	 function readAfterColon($string) {
    $array = explode(" ",$string, 2);
    return isset($array[0]) ? $array[0] : '';
    }

    function readLine($lineNumber, $fileName) {
        $file = file($fileName);
        return $file[$lineNumber-1];
    }

    function readLineFrom($lineNumber, $fileNames) {
        $lines = array();
        if(is_array($fileNames)) {
            foreach($fileNames as $fileName) {
                $lines[$fileName] = readLine($lineNumber, $fileName);
            }
        } else {
            $lines[$fileNames] = readLine($lineNumber, $fileNames);
        }

        return $lines;
    }

    function getFileNamesFromDirectory($directory = '.') {
        $fileNames = array();
        if ($handle = opendir($directory)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    $fileNames[] = $directory . $file;
                }
            }
            closedir($handle);
        }
        return $fileNames;
    }
	}
	//include the functions, then:
    //get the line from every file
    $descriptions = readLineFrom(1, getFileNamesFromDirectory('../uploads/likes/'));

    //get the contents
    arsort($descriptions);
    foreach($descriptions as $fileName=>$description) {
        $descriptions[$fileName] = readAfterColon($description);
    }

    //display it
    echo "<pre>\n";
    print_r($descriptions);
    echo "</pre>";
}

to get the job done. my question is, whats the easiest way to take those two files and get the top data and show it? Such as the post with 4 likes show as number one ect.

 

Also forgot to add theres three files per post.

 

Data file which has all the posts but just the main post then,

the thread file which has all the posts under that, then

the post likes file.

 

which are relevant theres one more for ips but thats for likes.

 

 

I have an idea of how to do the job but i feel as if its just insanely too much, plus, I write code a lot. I've been looking for a new community to join thats welcoming . I use to be apart of a php community before this that got shut down so, hi all! and thanks for any ideas before hand!

Link to comment
Share on other sites

Output the files in a ordered list?

echo "<ol>\n";
foreach($descriptions as $fileName=>$description)
{
    $likes = readAfterColon($description);
    echo "\t<li>$fileName has $likes likes</li>\n";
}
echo "</ol>\n";

Or use a counter

$i = 0;
foreach($descriptions as $fileName=>$description)
{
    $likes = readAfterColon($description);
    echo ++$i . ") $fileName has $likes likes<br />\n";
}
Edited by Ch0cu3r
Link to comment
Share on other sites

thats a step closer, i didn't clarify my question i guess, I'm try to open the contents of the file at:

 

../i/55b6baa475e85.txt ==> take the first line of file then add it to ==> ../data/top.txt in the order of top 10 posts.

 

the like name is the same as the file name.

Link to comment
Share on other sites

I've use text based files for some projects just because mysql was overloaded to begin with. They were very specific though.

 

Databases have many more advantages over flat file systems.

 

I would even say at least try a nosql solution versus trying to do it this way.

 

But to answer your question do it by the files date.

filectime

fileatime

filemtime

 

If you have them in an array can use ksort or any other sort function you require.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.