I'm writing some software that requires storing items in a database, the items need to have a 'priority' so we end up with
ID | Name | Priority
--------+--------------+----------
1 | Pear | 4
2 | Apple | 2
3 | Orange | 1
4 | Banana | 3
So now, the top priority fruit is the Orange, then Apple then Banana then Pear.
Now, I want to make Pear the number one priority so Pear, Orange, Apple, Banana. The table will look like:
ID | Name | Priority
--------+--------------+----------
1 | Pear | 1
2 | Apple | 3
3 | Orange | 2
4 | Banana | 4
Whats the best way to achieve this with PHP and Postgres. Given the table is not going to be more than about 12-13 items I've thought about SELECTing the entire table and rewriting the Priorities before UPDATING everything back.
* Important *
The priorities can be changed in any order, so priority 7 could be set to priority 3 (thus moving everything below priority 3 down a notch), and we need to close the gap the item with priority 7 which was moved to priority 3 has left in the priority list.
+1
- Though Postgres only supports functions, not stored procedures. – Cedillo