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.

    In reply to Building an index for next/last in a photo album. by Seumas

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.