First thing I see is most likely a mistake is
last if $count = $total_images;
That should be
last if $count == $total_images;
Now as for the real question. One perlish way to do it would be to query the database in whatever fashion you want and store the unique id's into an array. Since you already have a $count variable, it's easy to index when you need it.

I'm sure there's a much more efficient way to do this as this method would require you to query the database for each and every image. But it's an idea.

sub display_pictures { my $rowcnt = 5; my $colcnt = 5; my $total_images = $rowcnt * $colcnt; my $count = 0; my $tags_to_close = 0; # ensure all html tags are good my $stop; my $counted_in_row = 0; my @saved_results; print qq(<table>); my $data = qq(SELECT id, filename, title FROM pictures ORDER BY ID DES +C); my $sth = $dbh->prepare($data); $sth->execute() or die $dbh->errstr; my ($id, $filename, $title); $sth->bind_columns(\$id, \$filename, \$title); while ($sth->fetch) { push(@saved_results, $id); } print @saved_results; for( my $i=0;$i<$rowcnt;$i++) # for 1 .. 5 for rows { print "<tr>"; for(my $j=0;$j<$colcnt;$j++) # for 1 .. 5 for pictures { $counted_in_row++; if ($count == $total_images) { my $tags_to_close; $tags_to_close = $colcnt - $j; $tags_to_close = 0 if $tags_to_close == $colcnt; for (1 .. $tags_to_close) { print qq(<td> . </td> ); } $stop = 1; last if $stop; } #Retrieve your image from server here and #put the corresponding tags print qq(<td> $saved_results[$count++] </td>\n); # query the dat +abase via id here $count++; last if $count == $total_images; } print "</tr>\n"; last if $stop; } print qq(</table>); }


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

In reply to Re: Not sure how to query my database in this code by sulfericacid
in thread Not sure how to query my database in this code 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.