I’m building a photo gallery for a client, and I’d like them to be able to order the photos in it. I’m currently storing each photo in a photos folder and also storing information about them in a photos table in the database (with fields like filename, album id, creation time, etc.)
I’m trying to find the most efficient way an admin control panel could move the order of the photos around in the gallery after they are placed in albums.
Here is the solution I came up with:
- Use a precedence field in the photos table.
- The higher the precedence of the photo, the closer it will be to the top of the album.
- When the admin uploads a photo (we’ll call this new photo) then wants to move it before a previously uploaded photo (we’ll call this old photo) in the same album, PHP gets the precedence of the old photo, sets this value as the precedence of new photo, then finally shifts the precedence values of all the other photos in the album down 1.
- New photo will now appear before old photo in the album.
Simply, is there a more efficient way to accomplish this task?
Thanks in advance.
sounds good, but I would suggest that the lower the precedence is the closer to the top the photo will be. This will use the database indexes better and you won’t have to add “DESC” to your queries.