perl-embperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kee Hinckley <naz...@somewhere.com>
Subject Re: A "more" list
Date Sat, 14 Sep 2002 15:19:58 GMT
At 3:02 PM +0200 9/14/02, Kaare Rasmussen wrote:
>When listing rows, it is custom to have only 10 or 20 per page. What options
>exist for a "more" list like Google (1 - 2 - 3 - 4 ... next)? Or just a
>previous - next set?

Doing what Google does requires knowing how many results will be 
returned in total, which could be rather inefficient, depending on 
your database and the size of the queries.

DBIx::Recordset has builtin support for doing the prev/next option 
but it didn't give me quite the flexibility/transparency I wanted. 
What I did was this:

   [- $this->Commons->setupSearch(\%fdat, 20); -]

then do the search and print the results, and then

   <table border=0 width="90%">
     <tr>
       <td align=center width="50%">
         [$ if ($fdat{'$start'} > 0) $]
             <a href="[- $this->Commons->prevSearch() -]">Previous</a>
         [$ endif $]
       </td>
       <td align=center width="50%">
         [$ if ($$set->MoreRecords(1)) $]
             <a href="[- $this->Commons->nextSearch() -]">Next</a>
         [$ endif $]
       </td>
     </tr>
   </table>


The code for those follows.  You can replace the Embperlx::Util with 
ether HTML::Embperl or Embperl:: depending on whether you're using 
1.x or 2.x.  The Embperlx::Util package is one I wrote that looks to 
see which is loaded and maps the variables into the same name space 
so you can write code that works with both.  I intend to release all 
of this code here after I finish my current project.  It's currently 
driving four or five different database-driven, customer-updated web 
sites.

#
# Takes $%fdat, looks for $start and $max and sets them if they aren't there.
# The optional second argument sets the default value of $max--default is 10
sub setupSearch {
     my $this = shift;
     my ($fdat, $max) = @_;
     my $start;

     $start = $fdat->{'$start'};
     $start = 0 if ($start <= 0);
    
     if ($fdat->{'$max'}) {
         $max = $fdat->{'$max'};
     } else {
         $max = 10 if ($max <= 0);
     }
    
     $fdat->{'$max'} = $max;
     $fdat->{'$start'} = $start;
     $this->{fdat} = $fdat;
     return;
}  


# Optionally takes a url and fdat and constructs a "next" url.
# Uses the fdat cached by setupSearch and the url from SCRIPT_NAME
# if none are passed.
sub nextSearch {
     my $this = shift;
     my ($url, $fdat) = @_;
     my (%args);
    
     $fdat = $this->{fdat} if (!$fdat);
     $url = $ENV{SCRIPT_NAME} if (!$url);
     %args = %$fdat;
     $args{'$start'} += $args{'$max'};
     $this->printURL(\%args, $url);
}  
# Optionally takes a url and fdat and constructs a "prev" url.
# Uses the fdat cached by setupSearch and the url from SCRIPT_NAME
# if none are passed.
sub prevSearch {
     my $this = shift;
     my ($url, $fdat) = @_;
     my (%args);

     $fdat = $this->{fdat} if (!$fdat);
     $url = $ENV{SCRIPT_NAME} if (!$url);
     %args = %$fdat;
     $args{'$start'} -= $args{'$max'};
     $args{'$start'} = 0 if ($args{'$start'} < 0);
     $this->printURL(\%args, $url);

}

sub printURL {
     my $this = shift;
     my ($fdat, $url) = @_;
     my ($esc) = $Embperlx::Util::escmode;
     my ($key, $val, $res);

     $Embperlx::Util::escmode = 0;
     $res = "$url?" if ($url);
     while (($key, $val) = each %$fdat) {
         $res .= uri_escape($key) . '=' . uri_escape($val) . '&';
     }
     chop $res if (substr($res, -1, 1) eq '&');
     print Embperlx::Util::OUT $res;
     $Embperlx::Util::escmode = $esc;
}

-- 

Kee Hinckley - Somewhere.Com, LLC
http://consulting.somewhere.com/

I'm not sure which upsets me more: that people are so unwilling to accept
responsibility for their own actions, or that they are so eager to regulate
everyone else's.

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Mime
View raw message