Pfft. I imagine something like this;
PHP Code:
$thingId = 13; // The thing you are bumping, or moving, has an ID of 13.
$thingSortOrder = 2; // We are moving this item to position 2.
$sql = 'SELECT id, sort_order FROM things WHERE sort_order = "' . $thingSortOrder . '" LIMIT 1';
$result = $db->query($sql);
if ($result->numRows() != 0) {
// Something else has this sort order, so lets bump everything with a HIGHER sort order by 1.
$sql = 'UPDATE things SET sort_order = (sort_order + 1) WHERE sort_order >= "' . $thingSortOrder . '" '
$result = $db->query($sql);
}
// Now lets use the sort order that we wanted.
$sql = 'UPDATE things SET sort_order = "' . $thingSortOrder . '" WHERE id = "' . $thingId . '" LIMIT 1';
$result = $db->query($sql);
This will mean that you will eventually end up with sort orders which are not consecutive (eg: 1, 2, 7, 8, 9, 15, 16), but that should not really matter. This should be fine with a table of about 50 rows or so, with a maximum potential update of 49+1 rows, but this is an admin function and on average, those conditions will be pretty rare.