Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

HTML::Template/Pager or both?

by stew (Scribe)
on Jan 10, 2003 at 11:31 UTC ( [id://225768]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
I have built an index of a MySQL database using DBIx::FullTextSearch. I would like to provide previous/next and paged links. I have had a look at HTML::Pager and as I am also using HTML::Template this seems like the ideal solution.

I have poured over the documentation several times though I have to admit I'm no further forward. My first problem is DBIx::FullTextSearch only gives a resource id so I have to use this to query MySQL again and get some more interesting data such as resource title to show to the user. I can then put this into a hash and put this into a HTML::Template loop.

My first question then is.... which set of data do I apply HTML::Pager to the @files or @rows ? (see code below).


#!/usr/bin/perl use CGI qw(param); use DBIx::FullTextSearch; use DBIx::FullTextSearch::StopList; use DBI; use HTML::Template; my $dbh = DBI->connect('dbi:mysql:xxxx','xxxx','xxxxxx'); my $search = param("search"); my $fts = DBIx::FullTextSearch->open($dbh, 'fts_xxxx'); my @files = $fts->search($search); if (@files) { foreach $filename(@files){ $filename = substr($filename,0,-4); $sth = $dbh->prepare("SELECT id, title FROM resource WHERE id = +?"); $sth->execute($filename); while ($ref = $sth->fetchrow_hashref()){ $title = $ref->{title}; $id = $ref->{id}; push @rows, { ID => $id, TITLE => $title }; } } } my $template = HTML::Template->new(filename => 'pg_search.tmpl'); $template->param(TAB_BG_MAIN => "#0B5875"); $template->param(TAB_BG_HEAD => "#6180BA"); $template->param(SEARCH => $search); $template->param(ROWS => \@rows); print "Content-type: text/html\n\n"; print $template->output;


I'm not 100% about the call back routine mentioned in the docs and how I'm going to pass this to HTML::Template but if anybody can point me in the right direction I would be most grateful.
Many thanks
./stew

Replies are listed 'Best First'.
Re: HTML::Template/Pager or both?
by LTjake (Prior) on Jan 10, 2003 at 14:38 UTC

    Just to tell you up front, I've never used HTML::Pager, but it looks easy enough, and I use HTML::Template regularly.

    HTML::Pager only wants (in the end) the set of data to be displayed in the template. What I would do is get your @files array, then define the H::P callback function so it will only get extended info for the relevant items that are to be displayed on the page. Some code: (UNTESTED)

    my $get_data_sub = sub { my ($offset, $rows) = @_; my @return_array; my $sth = $dbh->prepare('SELECT id, title FROM resource WHER +E id = ?'); for (my $x = 0; $x < $rows; $x++) { my $filename = substr($files[$offset + $x], 0, -4); $sth->execute($filename); # Note I assume that the ID is unique (no need for a while loo +p - only # one record will be returned) otherwise the paging really doe +sn't work. $ref = $sth->fetchrow_hashref(); push @return_array, { ID => $ref->{id}, TITLE => $ref->{title} }; $sth->finish; } return \@return_array; }

    That's a start, anyway. You'll have to make your H::P template know that it has named vars for each line (TITLE and ID). I would also change the sub so that it takes more args (like the @items array and the $dbh database handler) so you don't have to reference global vars.

    HTH

    Update: Moved the $dbh->prepare(...) out of the loop.

    --
    "To err is human, but to really foul things up you need a computer." --Paul Ehrlich

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://225768]
Approved by IlyaM
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found