I posted a SOPW yesterday regarding how to make dynamic tables for an image gallery (whereas you can automagically print tables of with any number of rows and columns properly).

After playing around for most of the day today I got one that works great in the command line. I can't figure out from the code below how I'd fetch my database results. It may be due to work overload right now but I can't seem to grasp it. Can someone shine some light on this task for me?

Please forgive the not-so-perfect alignment, haven't gotten around to cleaning it up yet.

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 $data = qq(SELECT id, filename, title FROM pictures WHERE 1); my $sth = $dbh->prepare($data); $sth->execute() or die $dbh->errstr; print qq(<table>); my ($id, $filename, $title); $sth->bind_columns(\$id, \$filename, \$title); while ($sth->fetch) { 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; } print qq(<td> $id $filename $title </td>\n); $count++; last if $count = $total_images; } print "</tr>\n"; last if $stop; } } print qq(</table>); }

In reply to 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.