NYCPHP Meetup

NYPHP.org

[nycphp-talk] Problem with Pagination

Phillip Powell phillip.powell at adnet-sys.com
Thu Jun 3 16:25:42 EDT 2004


Joe Crawford Jr. wrote:

> Phillip,
>  
> here is your code, the red ( ) are the one's i added ;) let me know if 
> that worked.
>  
>         // 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>";
>          }



Ok, at long long last, after 2 whole days I successfully implemented 
Pagination on at least one of the Views.  One down, 12 more to go :(

here is the Pagination method displayPage() in its entirety.  Thanx for 
your help, I'm not that quick on complex mathematical algorithms..

    [PHP]
    /**
     * Display pagination links
     *
     * @access public
     * @param boolean $willHideAlbum (default false)
     * @return mixed HTML
     */
    function &displayPage($willHideAlbum = false) {                    
        // 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";
          if (!$willHideAlbum) $html .= " in \"$album\"";
          $html .= '</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;"; else $html .= 
"<a href=\"index.php?$qs&page=$i\">$i</a>&nbsp;";
          if ($i + 1 <= $numPages) $html .= '<b>|</b>&nbsp;';
         }

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

         // 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";
          if (!$willHideAlbum) $html .= " in \"$album\"";
          $html .= '</a>';
         }

         $html .= "\n</div>\n";
        }
        return $html;
    }
    [/PHP]
Phil



More information about the talk mailing list