I needed to adjust a value in a column in a database table by 1, for all rows. I found out you can do it easily by doing this:
-
$sql = "UPDATE images SET displayIndex=displayIndex +1 WHERE gallery_id=1";
Turning that into a function we can specify if we want to increment or decrement the value pretty easily. You can also provide a starting point for the adjustment (so if you delete the item with displayIndex=15, you only want to decrement the displayIndexes greater than 15 by 1) and specify a different value for the WHERE clause:
-
public function updateDisplayIndexes($galleryID, $startIndex, $add = true){
-
-
$operator = ($add) ? '+' : '-';
-
-
$sql = "UPDATE images SET displayIndex=displayIndex".$operator."1 WHERE gallery_id=$galleryID AND displayIndex> $startIndex";
-
}
There's a lot more you can do with it but it's been a pretty good starting point.