What are the best practices for maintaining image layout consistency in a PHP gallery system after deleting images?

When deleting images from a PHP gallery system, it is important to ensure that the layout consistency is maintained. One way to achieve this is by reordering the remaining images in the gallery after an image is deleted. This can be done by updating the image order in the database and then fetching the images in the correct order when displaying them in the gallery.

// Assuming $deletedImageId contains the ID of the image that was deleted

// Update the image order in the database
$query = "UPDATE images SET order = order - 1 WHERE order > (SELECT order FROM images WHERE id = $deletedImageId)";
// Execute the query

// Fetch the images in the correct order
$query = "SELECT * FROM images ORDER BY order";
// Execute the query and display the images in the gallery