View Full Version : Ordering a list

02-20-2008, 07:34 PM
Ok, it's actually in PHP. But the answer I need is just basic maths. I feel stupid but I can't figure out how to do it properly. I'm writing a forum and it works a charm at the moment. Only problem is order.

When a person submits the post, it sends the topic ID to the target page and then moves that topic to number 1 in the order list. So far example:

1st Topic | Order: 1
2nd Topic | Order: 2
3rd Topic | Order: 3
4th Topic | Order: 4

User posts in topic 4 so now it's:

1st Topic | Order: 2
2nd Topic | Order: 3
3rd Topic | Order: 4
4th Topic | Order: 1

Thats easy enough, I just +1 to all of them after making the chosen topic number 1. But if they choose topic 3 then this happens:

1st Topic | Order: 2
2nd Topic | Order: 3
3rd Topic | Order: 1
4th Topic | Order 5

The Order number can't be higher than the max ammount of topics, which is 4 in this example.

Theres probably a damn simple way of doing this that i'm just not seeing, and I feel really stupid because I don't usually have a problem with the maths side of things. What could I do to sort out this order problem?

Andrew Johnson
02-20-2008, 07:42 PM
You're making a mountain out of a molehill. Instead of doing it with some kind of order thing, add a timestamp column (let's call it `tstamp`) and just update it with the current timestamp every time you add a post to it. Finally, when you go to list the topics use a query such as:

SELECT * FROM topics ORDER BY tstamp

02-20-2008, 07:45 PM
Oh wow, I didn't think about it that way. Damnit! Thanks a million :D. Data table was already there for 'Last Post' so that takes care of 2 problems with one thing.