I arrange photos on an album by using a column "sort" in the table. This means that an album will be displayed with "ORDER BY sort".
I have a feature that allows users to arrange the photos on an album, by dragging and dropping photos, in javascript, and then pressing the "Save" button.
An array (with the sorted Photo IDs) is sent to the process (in PHP), and then I want to re-sort the rows on the table.
The easiest way in my mind to do is:
for ($c=0; $c<$length; $c++) {
mysql_query('UPDATE photos SET sort="'.$c.'" WHERE id="'.$array[$c].'"');
}
(please ignore sanitization, duplicates and other verifications here)
But I'm worried about the amount of queries that are made in a cycle like this.
How would you improve this approach?
Thank you.