Hi all!
I've been writing PHP code for quite a few years now, but I think I've just gotten in over my head. I'm a decent programmer, but I'm by no means a master.
I'm trying to write some code that will allow a user to arrange a group of links by their own preferance. The links are stored in a database, and I'd like to store a numerical value in the database as well to sort the links when the user views them (ie: link 1 goes at the top, link 2 underneath that, etc...).
Here's my problem: I'd like to make it as easy as possible on the user. If the user decides to move 'link 4' to the 'link 1' position I'd like the code to auto-adjust all the way down - filling the gaps - and then storing the new values in the database.
I'd guess my core question would be: How do I go about modifying the array to auto-arrange numbers and fill any gaps?
Any help, pointers or tips would be greatly appreciated. Thank you for reading this!
-Jack
Assigning/arranging relational numbers to an array
Moderator: General Moderators
-
blargalarg
- Forum Newbie
- Posts: 1
- Joined: Thu Aug 27, 2009 4:15 pm
Re: Assigning/arranging relational numbers to an array
Well two approaches would be to either look at it as an array or as a linked list. An array would be easier for a smaller number of records, but a link list would be ideal for a large number of records.
The general idea is that if you go with the array method, each record has an index (or a sort order). Now you can overlap with sort orders and then rely on their id's or timestamp for duplicate sort orders (... ORDER BY sort_order, timestamp, id ASC ...). This is the common method and I have seen it in many CMS'. I wouldn't really go down the path of updating all the other records in the database, but you could use a loop to do this (a stored procedure would be far more efficient). You would place the record into its new slot and then execute a loop to run through them all; resorting them.
The linked list method would require you to store the id of the row before and after the current record. When shifting a record to a new order, you would only have to update a handful of methods regardless of the number of records in the table.
The general idea is that if you go with the array method, each record has an index (or a sort order). Now you can overlap with sort orders and then rely on their id's or timestamp for duplicate sort orders (... ORDER BY sort_order, timestamp, id ASC ...). This is the common method and I have seen it in many CMS'. I wouldn't really go down the path of updating all the other records in the database, but you could use a loop to do this (a stored procedure would be far more efficient). You would place the record into its new slot and then execute a loop to run through them all; resorting them.
The linked list method would require you to store the id of the row before and after the current record. When shifting a record to a new order, you would only have to update a handful of methods regardless of the number of records in the table.