Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Not sure how to query my database in this code

by sulfericacid (Deacon)
on May 11, 2008 at 02:34 UTC ( #685920=note: print w/ replies, xml ) Need Help??


in reply to Not sure how to query my database in this code

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


Comment on Re: Not sure how to query my database in this code
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://685920]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (13)
As of 2014-07-23 17:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (148 votes), past polls