On Nov 17, A L said:

>1. How can I get to make results from mysql db to be displayed on web
>browser with pagination?  Meaning, when there are too many results from
>the db, it only displays 20 results at a time-like that of google-and
>show links to rest of the results by giving page numbers.  I've used
>Data::Page module from CPAN, but I can only get it to print out the
>result to the web statically without giving links to other pages and all
>the results are still displayed on one page when there should be 33 pages
>or more (the total pages number changes as more stuff are added to the
>db).  Also, DBIx::Pager from CPAN was looked (I'm still working on it to
>work), but it doesn't seem like it is not going to do what I'm expecting.
>Am I looking at the right places?  If not, will you tell me what I should
>be looking at?

The way I've done this is:

  use DBI;
  use CGI 'param';

  my $dbh = DBI->connect($dsn, $username, $password, { RaiseError => 1 });
  my $offset = param('offset') || 0;
  my $limit = 10;
  my ($prev, $next) = ($offset - $limit, $offset + $limit);
  $prev = 0 if $prev < 0;

  my $query = qq{ SELECT * FROM table WHERE f=? LIMIT $offset, $limit };
  my $n_query = qq{ SELECT * FROM table WHERE f=? LIMIT $next, 1 };
  my $arg = param('arg');

  my $sth = $dbh->prepare($query);
  $sth->execute($arg);

  while (my $rec = $sth->fetchrow_hashref) {
    # do stuff with $rec
  }

  $sth = $dbh->prepare($n_query);
  $sth->execute($arg);

  # if this is not the FIRST set of records
  print qq{<a href="search.cgi?offset=$prev&arg=$arg"><<<</a>}
    if $prev != $offset;

  # if there are more rows to show after these
  print qq{<a href="search.cgi?offset=$next&arg=$arg">>>></a>}
    if $sth->rows;

I expect that's more or less what DBIx::Pager does, but I've never used
the module.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to