Say I’ve 5 rows with id, catname and order fields:
+----+----------+-----+
| id | catname |order|
+----+----------+-----+
| 1 | cat1 | 2 |
| 2 | cat2 | 1 |
| 3 | cat3 | 3 |
| 4 | cat4 | 5 |
| 5 | cat5 | 4 |
+----+----------+-----+
and I want to update the order of these 5 categories from an array, e.g.:
array(1 => 3, 2 => 4, 3 => 5, 4 => 1, 5 => 2)
What is the best practice? To select each row and update order fields with its corresponding order in array? Or to create a new table containing order array of all these categories and use it for ordering and linking the two tables with a join statement using for example userid?
If you want to do it in one query, you can generate your SQL query as follows (assuming the keys are the existing
ordervalues):If the keys are the
identries, useCASE idinstead ofCASE order.