cammac Posted May 28, 2007 Share Posted May 28, 2007 When users can drag and drop photos on a webpage to sort their order in photo album, which version to use for storing the photo orders: Version 1: Table "Album Categories" AlbumId, AlbumName, UserID (3 / Family Album / 32) Table "Photos" PhotoId, UserId, AlbumId, PhotoOrder 39 / 32 / 3 / 1 56 / 32 / 3 / 4 94 / 32 / 3 / 2 92 / 32 / 3 / 3 78 / 32 / 3 / 5 Version 2: Table "Album Categories" AlbumId, AlbumName, UserID, PhotoOrder (3 / Family Album / 32 / 39,94,92,56,78 ) Table "Photos" PhotoId, UserId, AlbumId 39 / 32 / 3 56 / 32 / 3 94 / 32 / 3 92 / 32 / 3 78 / 32 / 3 Quote Link to comment https://forums.phpfreaks.com/topic/53291-which-2-table-structure-is-better-for-photo-order-storing/ Share on other sites More sharing options...
Illusion Posted May 28, 2007 Share Posted May 28, 2007 Version 1 Quote Link to comment https://forums.phpfreaks.com/topic/53291-which-2-table-structure-is-better-for-photo-order-storing/#findComment-263347 Share on other sites More sharing options...
cammac Posted May 28, 2007 Author Share Posted May 28, 2007 Any suggestions how to update the photo order when e.g. photo #99 would be dragged to position #1 - that would mean 99 rows would have to be updated in MySQL (instead of only 1 in Version 2) Quote Link to comment https://forums.phpfreaks.com/topic/53291-which-2-table-structure-is-better-for-photo-order-storing/#findComment-263421 Share on other sites More sharing options...
Wildbug Posted May 28, 2007 Share Posted May 28, 2007 UPDATE Photos SET PhotoOrder=PhotoOrder+1 WHERE UserId = 32 AND AlbumId = 3; UPDATE Photos SET PhotoOrder=1 WHERE UserId = 32 AND AlbumId = 3 AND PhotoOrder = 100; You're right, though. Version #1 may mean more database operations. It does, however, provide an easy way to get the pictures out in order (by using the PhotoOrder as the ORDER BY column when retreiving photos). You wouldn't get that functionality with Version #2. With Version #2 you can use explode() to read that delimited string into an array and write your own re-ordering functions. And, since you're probably not pulling the pictures out in order, but singly (img src="display.php?id=12345"), you might not need MySQL's ORDER BY. Yeah, actually Version #2 might not be a bad itea. This is one of the few cases where I can see some advantages to using the delimited string, and they're mostly related to performance. Quote Link to comment https://forums.phpfreaks.com/topic/53291-which-2-table-structure-is-better-for-photo-order-storing/#findComment-263444 Share on other sites More sharing options...
cammac Posted May 28, 2007 Author Share Posted May 28, 2007 I'm also thinking that with version 2 when I would select the comma delimited string and then go through the comma separated array as I would go through various rows in version 1, then e.g. if I would display 20 photos per album page, I would have to do only 1 mysql row selection instead of 20 rows in version 1, and just limit the comma separated list to 20 first items. So in theory 1 write instead of 99 writes, 1 read instead of 20 reads. Am I correct in my theory? Or is there a downside I'm not seeing. Quote Link to comment https://forums.phpfreaks.com/topic/53291-which-2-table-structure-is-better-for-photo-order-storing/#findComment-263463 Share on other sites More sharing options...
bubblegum.anarchy Posted May 28, 2007 Share Posted May 28, 2007 Using version #2 and the following query to order the records: SELECT FIND_IN_SET(PhotoId, PhotoOrder) AS orderby, Photos.* FROM Photos INNER JOIN `Album Categories` ON Photos,AlbumId = `Album Categories`.AlbumId ORDER BY orderby; Quote Link to comment https://forums.phpfreaks.com/topic/53291-which-2-table-structure-is-better-for-photo-order-storing/#findComment-263532 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.