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
-
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.