CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in
Add ListingModify ListingTell A FriendLink to TPASubscribeNew ListingsCool ListingsTop RatedRandom Link
Newest Reviews
  • review
  • hagen software
  • NOT GPL!
  • Hagan Software
  • Wasted Time with ...
  • poor pre-sale sup...
  • no response
  • rating the offer
  • Good Stuff
  • Good idea but use...

    Perl Archive : TLC : Programming : Perl : Multiple Search Results Pages in Perl
    Guide Search entire directory 

    Date Published: 2000-05-01

    Written for the Perl Archive by Ray Street

    You have often seen search results split into pages with 10 results per page - all the search engines do this. There are links near the foot of each page to the next and previous pages so when you click on them you jump to the required page.

    How can this be done in Perl?

    It's easy - I'll show you a simple way to do this.

    First of all, you need your results. You usually get these from a file of some kind. For the purposes of this article, I'll assume that you've found all of the details and have them stored in an array called @results (this array would probably hold the keys of the file records so that you can access the records again when you show the results pages). You've also stored the number of results in $result_count.

    Now decide how many results in a page - it's usually 10 but if you're showing a lot of graphics you might want to use 5. Put the count into $pagesize ($pagesize = 10;) - if you use $pagesize throughout the script then you can experiment with different numbers of results on a page by changing this single line of code.

    Work out how many pages you're going to show as follows:-

    if ($result_count != 0) {
    $pagecount = int($result_count / $pagesize);
    if (($pagecount * $pagesize) != $result_count) {

    Now you want to know which page of results you're about to show. The links at the foot of the page are going to call this same script with a field called reqpage - if reqpage is not defined (which means that this is the first time that the current script has been used) then set $reqpage to 1, otherwise set $reqpage to the input field value.

    Now work out which is the first and last result that you want to show by using the $reqpage and $pagesize values.

    $firstresult = (($reqpage - 1) * $pagesize) + 1;
    $lastresult = $firstresult + $pagesize - 1;
    if ($lastresult > $result_count) {
    $lastresult = $result_count;

    The adjustment of $lastresult stops you from running off the end of the array on the last page if there are not $pagesize number of results to show.

    You can now output your results. This is going to vary for every application but usually involves you writing a processing loop to work from the $firstresult element in the @results array to the $lastresult element (reading the file/database again as necessary and generating the HTML code). If you want, you can output a heading before the results saying something like "Results $firstresult to $lastresult of $result_count" so that the total number of results is shown and the current position within the result pages is obvious - this is a very good idea if the web page is more than a screen in size (a lot of results pages are more than a screen).

    So the results are shown and now you need to put in the links.

    I've chosen to use text words ("Previous" and "Next") with arrows for people who want to work backwards and forwards through the results without worrying what the current page is - you could equally use a graphic. The word "Previous" is not shown if you're on the first page and "Next" is not shown if you're on the last page.

    I also show the current page number as bold without it being a link - this lets the person easily see what page they're on.

    First of all, set up the "Previous" and "Next" details:-

    $prev_page = $reqpage - 1;
    $next_page = $reqpage + 1;
    if ($reqpage == 1) {
    $prev_link = "";
    } else {
    $prev_link = " <a href=\"http://domain/cgi-bin/results.cgi?reqpage=$prev_page&pagesize=$pagesize\">" . "PREVIOUS" . "</a>";
    if ($reqpage == $pagecount) {
    $next_link = "";
    } else {
    $next_link = " <a href=\"http://domain/cgi-bin/results.cgi?reqpage=$next_page&pagesize=$pagesize\">" . "NEXT" . "</a>";

    You're now ready to create the links - remember that you don't do this if there is only one page of results (a bit pointless really):-

    if ($pagecount > 1) {
    $pagelinks = $prev_link;
    $pageno = 0;
    while ($pageno < $pagecount) {
    if ($pageno == $reqpage) {
    $thislink = " <b>$pageno</b> ";
    } else {
    $thislink = " <a href=\"http://domain/cgi-bin/results.cgi?reqpage=$pageno&pagesize=$pagesize\">" . $pageno . "</a>";
    $pagelinks = $pagelinks . $thislink;
    $pagelinks = $pagelinks . "   " . $next_link;
    } else {
    $pagelinks = "";

    So all you have to do now is output the $pagelinks variable to the HTML and the links are there.

    That's it. Told you it was easy.


    About The Perl ArchiveLink Validation ProcessSearch Tips
    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives