NYCPHP Meetup

NYPHP.org

[nycphp-talk] Problem with Pagination

Phillip Powell phillip.powell at adnet-sys.com
Thu Jun 3 13:45:40 EDT 2004


I wrote a class method that will handle pagination regarding 
medium-sized resultsets (estimating maximum number of rows at 500).  
Everything works beautifully, except one annoying problem that involves 
someone with a math degree or really really good with numbers:

The "next link" displays the wrong number of "next" items every time!  I 
can't honestly, for the life of me, figure out the algorithm to get it 
right.  The pagination functionality works perfectly, except that one part.

Here is the "Reader's Digest" version of my class:

[PHP]
|class PaginationView extends View {

         var $result;   // YOUR RESULTSET

        function PaginationView($result) {   // CONSTRUCTOR
                $this->result = $result;
        } 

    function &displayPage() {                            // STATIC HTML 
STRING METHOD
        global $section, $action, $album, $headerMenuArray, $willPaginate, $displayItemLimit; 

        foreach ($_REQUEST as $key => $val) if (!isset(${$key})) ${$key} = $val; 


        $qs = "section=$section&action=$action&sort=$sort&chooseAlbum=1&album=" . urlencode($album) . '&willKeepPageSession=1';    // 
FOR EASE OF WRITE

                if ((int)$page === 0) $page = 1;
        $page = (int)$page;        // CONVERT TO INTEGER

            if (@sizeof($this->result) > $displayItemLimit && $willPaginate) { 


         $html .= "<div align=\"center\">\n"; 

         // PREVIOUS LINK
         if ((int)$page !== 1) {
          $pagePrev = $page - 1;
          $html .= " <a href=\"index.php?$qs&page=$pagePrev\">Previous $displayItemLimit ${section}s in \"$album\"</a>&nbsp;<b>|</b>&nbsp;"; 

         } 

             $numPages = (int)(@sizeof($this->result) / $displayItemLimit);
            
         // ALL PAGES (PAGE NUMBER) LINK(S) EXCEPT FOR LAST PAGE
         for ($i = 1; $i <= $numPages; $i++) {
          if ((int)$i === (int)$page) $html .= "$i&nbsp;<b>|</b>&nbsp;"; else $html .= "<a href=\"index.php?$qs&page=$i\">$i</a>&nbsp;<b>|</b>&nbsp;"; 

         }

        // LAST PAGE NUMBER LINK
         if (@sizeof($this->result) % $displayItemLimit != 0) {
          if ((int)$i === (int)$page) $html .= "$i&nbsp; "; else $html .= "<a href=\"index.php?$qs&page=$i\">$i</a>&nbsp; "; 

         }

        // NEXT LINK
         $offset = (int)(@sizeof($this->result) - ($displayItemLimit * ($page - 1))); 

         if ($offset > 0) {        
          $pageNext = $page + 1;
          $html .= "&nbsp;<b>|</b>&nbsp;<a href=\"index.php?$qs&page=$pageNext\">Next "; 

          $html .= ($offset + (int)$displayItemLimit > @sizeof($this->result)) ? (int)($displayItemLimit - $offset) : $displayItemLimit; 

          $html .= " ${section}s in \"$album\"</a>";
         }

         $html .= "\n</div>\n";
        }
        return $html;
    }

}

|

-- 
[/PHP]
Here is a sample output that results with a $displayItemLimit of
20 items and I'm on page "1" and there are 45 items altogether:





    quote:
    ------------------------------------------------------------------------
    1 | 2 | 3 | Next -25 images in "Album 1"
    ------------------------------------------------------------------------





The problem is the $offset variable I know in the // NEXT LINK code
block, but I'm stuck, I can't figure it out, furthermore, both of these
utterly fail in PHP 4.3.2 on my system:





    |PHP:|
    ------------------------------------------------------------------------
    |
    $pagePrev = $page--;
    |
    ------------------------------------------------------------------------


    |PHP:|
    ------------------------------------------------------------------------
    |
    $pageNext = $page++;
    |
    ------------------------------------------------------------------------



Both result in $pagePrev and $pageNext having null values even though 
$page exists and is cast into an integer

-----------------------

At this point I'm not sure what else to do since I'm close to implementing my Pagination class instantiation on my other display classes, but not until I get this one problem fixed or someone just guide me in the right direction as to the exactly working algorithm for at least the "next" links to display correctly.




	*Update*

I am basing my algorithm on the tutorial at 
http://www.phpfreaks.com/tutorials/43/4.php

Thanx
Phil
---------------------------------------------------------------------------------
Phil Powell
Multimedia Programmer
BPX Technologies, Inc.
#: (703) 709-7218 x107 
Fax: (703) 709-7219

	




More information about the talk mailing list