NYCPHP Meetup

NYPHP.org

[nycphp-talk] Problem with Pagination

Phillip Powell phillip.powell at adnet-sys.com
Thu Jun 3 15:58:55 EDT 2004


Joe Crawford Jr. wrote:

>Phillip,
>
>after taking a look at your code i have come to this conclusion and may be
>incorrect
>
>your code :
>
>$html .= ($offset + (int)$displayItemLimit > @sizeof($this->result))
>
>shouldnt that be
>
>$html .= (($offset + (int)$displayItemLimit) > @sizeof($this->result))
>i have added the parenthesis around
>$offset + (int)$displayItemLimit
>so that the addition get's completed before the > operator is evaluated.
>
>let me know if that fixes the problem
>  
>

I'm sorry I'm a bit confused as to where you pulled that information 
from.  Here is the snippet now that correctly produces the "next" link:

         // NEXT LINK
         $pageItemCount = (int)(@sizeof($this->result) - ($page * 
$displayItemLimit));
         if ($page <= $numPages && (int)$pageItemCount > 0) {       
          $pageNext = $page + 1;
          $html .= "<b>|</b>&nbsp;<a 
href=\"index.php?$qs&page=$pageNext\">Next ";
          $html .= ($pageItemCount < $displayItemLimit) ? $pageItemCount 
: $displayItemLimit;
          $html .= " ${section}s in \"$album\"</a>";
         }

They need to make pagination easy.

Phil

>Joe Crawford Jr.
>
>
>----- Original Message ----- 
>From: "Phillip Powell" <phillip.powell at adnet-sys.com>
>To: "NYPHP Talk" <talk at lists.nyphp.org>
>Sent: Thursday, June 03, 2004 1:45 PM
>Subject: [nycphp-talk] Problem with Pagination
>
>
>  
>
>>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
>>
>>
>>
>>_______________________________________________
>>talk mailing list
>>talk at lists.nyphp.org
>>http://lists.nyphp.org/mailman/listinfo/talk
>>
>>
>>    
>>
>
>_______________________________________________
>talk mailing list
>talk at lists.nyphp.org
>http://lists.nyphp.org/mailman/listinfo/talk
>
>  
>


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

	




More information about the talk mailing list