There have been a few similar problems here on SO, but I had already implemented my way; Thought this is far from what would be expected "good"; The problem is that I have a list, you can drag & drop, though if one drags 1 element from position to position 50, 49 elements need to get updated (this would be 49 queries) just to save 1 action; Now to not do this, I saved a list and save the list to mysql;
+---------+------------+-----------+
| user_id | project_id | user_sort |
+---------+------------+-----------+
| 1 | 2 | 3,1,2 |
+---------+------------+-----------+
This has the advantage, I only need to update 1 line; The issue however is when user returns, I need to do 2 queries (select user_sort, and select all id's); and use PHP in between to explode(user_sort); most unclean and prolly slow.
Is there a better way to do this ?
if one drags 1 element from position to position 50, 49 elements need to get updated (this would be 49 queries) just to save 1 action
That is not true, you can update all 49 rows in 2 queries:
$id = 10; // moved element
$old_pos = 1;
$new_pos = 50;
// elements between old and new position
if ($new_pos > $old_pos) {
$sql1 = "
UPDATE my_table
SET user_sort=user_sort-1
WHERE user_sort > $old_pos
AND user_sort <= $new_pos";
} else {
$sql1 = "
UPDATE my_table
SET user_sort=user_sort+1
WHERE user_sort >= $old_pos
AND user_sort < $new_pos";
}
// moved element
$sql2 = "UPDATE my_table SET user_sort=$new_pos WHERE id=$id";