Suppose you have an image gallery stored in a mysql database, the field id is your index.

How do you calculate previous/next images from the database (assuming you can't just go ++ or -- as the admin can delete images and thus you could have image16.jpg and the next image in the database be image32.jpg).

My idea was to query the db first and store ALL the picture id's in that category into an array. That way I know what's available. Then query for the picture, then somehow see if there is a previous/next image.. and if so, what it is.

Can someone point me in the right direction?

# prequery to see if there IS a previous/next id my $data = qq(SELECT id, title FROM pictures WHERE category = $categ +ory ORDER BY id ASC); my $sth = $dbh->prepare($data); $sth->execute() or die $dbh->errstr; my @pictures_in_category; my ($picid, $pictitle); $sth->bind_columns(\$id, \$title); while($sth->fetch) { push(@pictures_in_category, $id); } # real query for the one image you're viewing my $data2 = qq(SELECT id, filename, title, category, description, da +te, views, sensorisospeed, exposuretime, isospeedrating, lensaperture, flash, focallength, cameramodel, shutter +speed FROM pictures WHERE id=?); my $sth2 = $dbh->prepare($data); $sth2->execute($picture) or die $dbh->errstr; ...

In reply to Calculating previous/next from database by Anonymous Monk

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.