views:

64

answers:

4

Hi there,

I'm working on a file based commenting system with in-line comments (only 1 level). Despite being a newby I've managed to create a system in which users are able to add new comments and use @[NUMBER] to add their reply below another comment.

My file and folder structure looks like this:

/threads/
         1/
            1.txt
            2.txt
            3.txt

         2/ 
            1.txt
         3/
            1.txt
            2.txt
            3.txt
            4.txt

The folder in threads has the thread number (used to make a reply) for its name and 1.txt contains the contents of the "mother" post. Every post higher than 1 is a reply.

So I could show the threads in the order which they have been made or show them upside down, but they'll be stuck in that order (I'm using a loop to find folders and then increase or decrease the folder number). Does anybody have any ideas on how I can make threads which get replies to the top of the list?

I thought of an order.txt file which has the thread numbers in a certain order and when a reply gets made to thread X the script should put X at the top of that list (or bottom, easy to inverse).

Suggestions are very much appreciated!

+1  A: 

Stick a file in each folder (1, 2, 3, etc...) that contains a line with the time that the thread was last updated (could be when it was posted if it has no replies or the time of the last reply). When it's time to display each thread, look at the time in that file and slot it into the correct position when displayed.

The reason I'd do this over creating an order.txt file is that a single file is:

  1. Easily corrupted, meaning that you'd lose ALL ordering info.
  2. It is inefficient to read/rewrite a large file everytime a post is added

You can also stick other data you might want into this text file in each folder. It's kind of like how Windows stores thumbnails in a thumbs.db file for each folder.

box9
Thanks a lot, but (just like for the first answer) I wouldn't really know how to properly display them after I've got all timestamps. First, I need all timestamps to determine what comes first, then I have to determine the order somehow and output the files corresponding to the timestamps (which I have put into order)...? Thanks!
Chris
+1  A: 

Well, while I don't agree with the wisdom of doing it this particular way, the order text file seems fine enough granted depending how often your updating your threads there may be read/write lock issues.

Another option is to check the modified time for the folders and/or posts. http://php.net/manual/en/function.filemtime.php i believe would be an appropriate function.

In reference to sorting you'd need to use a sorting function. The best ideas i can come up with is when creating your array convert the modified time to a unix timestamp and use that as the array index. May need to invert the array after to show newest first but i would think that would work. I'd suggest using the mktime function in conjunction to produce a nicely formatted date/time and then use that for the index.

An alternative is storing an array within array and doing something like this:

$threadArray = array(
   array("thread" => "1", "timemodified" => "12703048849"),
   array("thread" => "2", "timemodified" => "12703048842"),
   array("thread" => "3", "timemodified" => "12703045349"),
   array("thread" => "4", "timemodified" => "12703021449"),
);

function sortByTime($a, $b)
{
  return strnatcmp($a['timemodified'], $b['timemodified']);
}

# sort
usort($threadArray, 'sortByTime');
CogitoErgoSum
Hmm yes, and I could use Touch() to update the 1.txt file so I don't have to check all of the files and see if they've been updated lately.Thank you. But how do I get them displayed in the right way, how do I "sort them by time". Thanks again.
Chris
Check my edits i added a blurb on that
CogitoErgoSum
Thank you for your edit!
Chris
+1  A: 

Pretty similar to box9, but with a different approach.
In each thread folder, make a file, let's say - random.timestamp.txt ($file = 'random.'.time().'.txt';).
Whenever there are changes within the thread, you rename this file with the new timestamp. When you're displaying threads, fetch each threads timestamp file and align them as you wish (DESC / ASC). Something like that..

Tom
Is there anyway to easily compare timestamps? Wondering how I could trace the timestamps back to their threads once I have retrieved them all... Thank you for your response!
Chris
Since timestamps are just numbers, you can compare them as you wish. `>`, `<`, `=`, `!=` etc. But I'm not sure if I understood your question perfectly..
Tom
Yes that's what I meant, just figured that it'd be harder because of the HH:SS DD/MM/YYYY format.
Chris
Timestamp isn't a HH:DD DD/MM/YYYY format. Try to echo `time()` in PHP. Plus, you can read http://www.unixtimestamp.com/index.php for more info about timestamps.
Tom
Ah sorry, I was confused by date().
Chris
+1  A: 

I'll probably get down voted for this, but if you're not going for a relational database here, why not make the order information part of the file/folder name? So you could order by metadata that is contained as part of the file/folder name scheme (as you have started to do here) your threads directory would contain a 1_2/ (first thread, second order) 2_1/ (second thread, most recent), 3_0/ (third thread, sticky <--please forgive the feature creep) This would allow you to use the split function to get relevant metadata from the file names.

Final folder structures would vary and change on update, but might look something like this in a snapshot:

/threads/ 
          1_2/
               1.txt
               2.txt
          2_3/
               1.txt
               2.txt
               3.txt
          3_1/ 
               1.txt
               2.txt
fauxtrot
Read Write locks would impact this. I.e im tryin gto update a folder called 1_200901011155 and its been changed already to 1_200901011159Other issues with this to i won't get into but its an intriguing idea.
CogitoErgoSum
Yes, I've run into that issue myself. If the scope of the project dictates that kind of accuracy and volume of interaction, it might also indicate the need for a relational database. I like your idea for using the operating system's file metadata.
fauxtrot
Ah I should have thought of this instead of my "order.txt" file idea, interesting, thank you :-)
Chris