I’ll be returning values from my sorting script, which provides the position of the element at the beginning and at the end of the change. So let’s say 4th position goes to 1st. Knowing that I just need to add 1 to the original 1st,2nd, and 3rd position to retain the order.
Now, what’s the best way of updating the database? Is it to run a queries in a loop for every id that needs to be changed? That’s 4 update queries, which seems inefficient? Is there another way?
I have done something similar to what I think you’re trying to do. I had a table where each row represented a certain UI element to be laid out on the page in a grid. The user was able to change the order of the UI elements, and I would store that number in the table so that it would come up in that order by default.
For this, if I recall correctly, I used two queries:
First query sets that particular one to the lowest number, second query increments all the rows under the original value by 1 (including the one you just set to 0, making it 1).