I'm working on something where users can rearrange items, and at a later time, those items need to be displayed in the order chosen. As a simple example, consider a list of items:
A, B, C, D, E, F, G.
The MySQL table would be something simple: user_id, letter, sortnumber
The user is allowed to change the order in incremental steps. They might move A to after D, G to the beginning, etc. In addition to this, they can add and remove items. So they might delete C, or add X. In each of these steps, I send data to PHP, which will process it, and set the items in MySQL.
There are two ways I see going about this:
- Each time they add/remove/reorder something, send the entire list to PHP, delete all the data they previous had in there, and just insert the new list. Problem is, this is a lot of deletions/insertions each time they do anything. They might move A to after B, and then suddenly I delete 7 records, and insert 7 more. On the plus side, it's dead simple.
- Each "move" they do (eg: an add, or a remove, or a reorder), send the information for that. EG, they moved A after F, and tell me "move A after F" I now have to check that both A and F exist on the list, then I have to decrement all "sortnumber" between A and F (including F). If they say "delete Z" I have to find it on the list, delete it, and decrement all sortnumbers of records after it.
So I'm just curious... has anybody had to deal with something where order matters, and if so, how did you go about it?