laserlight;10903713 wrote:I think you want to do this:
1. Select the PhotoAlbumID and PhotoDateAdded corresponding to the photo with the given PhotoID.
2. Select the number of photos with the retrieved PhotoAlbumID such that the PhotoDateAdded is less than the retrieved PhotoDateAdded.
3. Add one to the result.
This can be accomplished with a single SQL statement, e.g.,
SELECT COUNT(*) + 1
FROM tbl_members_photos A, tbl_members_photos B
WHERE A.PhotoID=:PhotoID AND A.PhotoAlbumID=B.PhotoAlbumID AND B.PhotoDateAdded < A.PhotoDateAdded;
where 😛hotoID is a placeholder for the given PhotoID.
By the way, if $ThePhotoID is an integer, you should cast it to int instead of using mysql_real_escape_string().
Thanks for your time on this! I think your solution is close to what I need, I'll explain further the system.
I have two tables, one for Photos, one for Albums - within the Photos table is a column to associate each picture with an album.
I also have a neat Pagination script so as for the user to page through the photos - this script requires a 'page' parameter so as to jump to a specific point in the photo recordset, filtered by the AlbumID.
If the user starts at the beginning of the Album, I can simply create the recordset and let them paginate through BUT to start the user midway through the album, jumping straight to a specific photo, I need to know at what stage that photo appears in the recordset (that is ALWAYS sorted DESC by PhotoDateAdded).
Therefore, what I'm trying to achieve is a way to find the Row Number where that photo appears, starting from a base of '1', so as to be able to pass a page number to my pagination script.
The result of this script is there...
- $page (the rowid of the relevant photo within the recordset)
- $albumID (I need to get this out of the recordset for the pagination SQL)
Does that make sense? Can I achieve all of this within one SQL script?
Thanks again for your help!
p.s. What does the := do in SQL?