stonecolddevin has asked for the wisdom of the Perl Monks concerning the following question:

Ok, here's the situation:
First of all, it's QUITE apparent to me I need to switch to Class:DBI, but for not, DBI will have to do.
Ok, I'm trying to figure out how to retrieve a reply count on a thread, and I know the sql needed, and I know how to do it without HTML::Tenplate.
SQL code:
SELECT COUNT(*) FROM replies WHERE reply_id=? $sth->execute($q->param('id')

This involves a while loop, that which I cannot figure out how to incorporate it into HTML::Template.
Thank you kind, and wise monks, forgive my if this is vague, 3 300m dashes along with a cocktail of other grueling sprints in track tends to dull my question asking abilities.
-dhoss
All posts are spawns of an archaeic and instinctual remnant of pre-neanderthalian neural synapse that, while irrevocably human, remains an anomoly to the common laws of nature.

Replies are listed 'Best First'.
Re: Retrieving a message count on a thread using HTML::Template, and DBI.
by matija (Priest) on Mar 31, 2004 at 05:59 UTC
    Incorporate it into HTML::Template? Well, you do the DBI calls in your Perl code, then you pass the calculated value as a param to the template:
    my $rc=$sth->execute... my $tmpl=HTML::Template->new... if ($rc==1) { my ($count)=$sth->fetchrow_array; # I only use this when fetching a +single value # otherwise I use fetchrow_hashref $tmpl->param(replycount=>$count); } ... # lots more code, presumably print $tmpl->output;
Re: Retrieving a message count on a thread using HTML::Template, and DBI.
by Mr. Muskrat (Canon) on Mar 31, 2004 at 03:35 UTC

    Perhaps something like this?

    ... my $sql = 'select count(*) from replies where reply_id=?'; my $sth = $dbh->prepare($sql) or die $dbh->errstr; ... my $rc = $sth->execute($q->param('id')) or die $sth->errstr; my ($count) = $sth->fetchrow_array if ($rc == 1); ...

Re: Retrieving a message count on a thread using HTML::Template, and DBI.
by simonm (Vicar) on Mar 31, 2004 at 18:55 UTC
    This involves a while loop, that which I cannot figure out how to incorporate it into HTML::Template.

    Fetching a single row from something like a "select count(*) from..." query doesn't actually require a loop, just a single fetch and then finish.

    However, if you were looping over multiple rows a loop would be in order. You could either use HTML::Template's <TMPL_LOOP> tags to do your looping, or you could go through the loop ahead of time, concatenating all of the output into a single variable that you'd pass to your template.