Seumas has asked for the wisdom of the Perl Monks concerning the following question:

I slept on this problem and woke up without a solution, so I'm finally turning to my fellow monks.

I have a photo album system. When you submit a URL where part of the query contains img_id=# - the img_id is used to do a lookup in the database and pull out the information on that record, including its disk location. Then this image is displayed on a page by itself.

On the page with the photo, I want to display a preview thumbnail of the photo just before this one and after so they can be clicked to progress through the system.

The problem is, I can't figure out how to pull the target record, plus the one before and after it in a single SQL query. I suppose that can't be done. So I decided as a workaround, I would do a selectall_arrayref() against the album_id and build an index out of it. Then I could find what index number the target record is in, in that array. TO get the previous and next images, I could subtract and add one from the index and grab the values out of them.

For example, I want to get img_id #43 from the database, so:
  • I do a fetchrow_array() to get the album_id it belongs to.
  • Then I do a select_all_arrayref() to get every img_id from the database that belongs to album_id.
  • Then I want to stuff all the returned img_id's into an array and use index @array, $img_id to find it's location in the index. To get the image that comes before and after it, I then -- and ++ the results.

    The problem, of course, is that selectall_arrayref is an arrayref and to do this, I'll need just a regular array. So what would be the quickest/shortest way to turn the $album_idx = $dbh->selectall_arrayref() into just an array?

    Or... could anyone with more experience offer another solution to my whole confounded "building an index of img_id's from the database" to keep track of the pevious/next images?

    Edit: I'm using PostgreSQL.