swa66 - 12:32 pm on Nov 19, 2012 (gmt 0)
I suppose your article come out of a database somewhere. Correct ?
Next and previous imply that you have a scheme that defines an order of things.
Do you have more than one such a scheme ?
Can you define the order easily ? Can you explain what you want at the ends (the fist and last) ?
Data structures such as arrays are pretty easy to find next and previous in, but other means exist as well: you can build a linked list (each entry contains a pointer to the next item).
That way it can be "walked" in one direction.
To walk it in both directions, a double linked list can be built, but take care: maintaining the links quickly becomes difficult as you have more of them to maintain.
If you store it in SQL, you can actually query the database to give you the next and previous one in the given order (as long as you can define the order) - no programming needed: SQL does the heavy lifting for you. It's based on joining the table with itself, where you impose that the returned rows comply to be prior or later, ordering the results in the order you define, you then limit it to return only one row t get teh one row that's closest and satisfies the direction you seek.