Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others learning in the Monastery: (5)
    As of 2024-04-19 17:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found