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

Hi monks,

I tried to use HTML::Pager to display my database contents. The contents are displayed but navigation doesnot work. I have given my code below.

#!/usr/bin/perl #uses use strict; use warnings; use diagnostics; use DBI; use CGI; use HTML::Pager; #Declare Variables my $query=CGI->new(); my $count=0; #Fetch DB Connects my $get_data_sub = sub { my (@return_array,@data); #connect db & prepare stmt my $dbHandle = DBI->connect("dbi:mysql:tempdb","user","password +") or die ("Cannot Connect to database"); my $stmtHandle = $dbHandle->prepare("select sno,name,age,deptno + from sample1"); #fetch content from Database $stmtHandle->execute(); while(@data=$stmtHandle->fetchrow_array()) { push(@return_array, [@data]); } #disconnect db $stmtHandle->finish(); $dbHandle->disconnect(); return \@return_array; }; #Pager my $pager = HTML::Pager->new(query=>$query, get_data_callback => $get_data_sub, rows => 11, page_size =>5, cell_space_color => '#000000', cell_background_color => '#ffffff', debug => 1 ); print "Content-Type: text/html\n\n"; print $pager->output;

Thanks, arunvelusamy

Replies are listed 'Best First'.
Re: HTML::Pager, Navigation not working
by perl_lover (Chaplain) on Oct 24, 2005 at 12:51 UTC
    Hi Arun,

    You should specify the number number of rows in the resultset to the rows key. You have hardcoded like rows => 11. Check with HTML::Pager POD and then update accordingly.

    -perl_lover-
Re: HTML::Pager, Navigation not working
by sacked (Hermit) on Oct 24, 2005 at 20:51 UTC
    From the HTML::Pager docs:
    get_data_callback - this is a callback that you provide to get the pages of data. It is passed two arguements - the offset and the number of rows in the page. You return an array ref containing array refs of row data. For you DBI-heads, this is very similar to selectall_arrayref() - so similar that for very simple cases you can just pass the result through.
    In addition to the problem described by perl_lover above, your get_data_callback ignores the paging variables (offset and row count). Since you are using MySQL, you should be able to use the LIMIT modifier to your select statement to handle paging:
    my $get_data_sub= sub { my ( $offset, $rows ) = @_; # ... my $stmtHandle = $dbHandle->prepare(<<" END"); SELECT sno, name, age, deptno FROM sample1 LIMIT $offset, $rows END # rest of code as before

    --sacked