NYCPHP Meetup

NYPHP.org

[nycphp-talk] Problem with Pagination

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


This is per Joe Crawford's request.

Phil

[PHP]
// CLASS FOR IMPLEMENTATION FOR VIEW CLASSES
class View {

    /**
     * Abstract class for views
     * @abstract
     */
    function View() {                                                    
                    // "CONSTRUCTOR"
        // PREVENT INSTANTIATION OF THIS OBJECT TO ENSURE "ABSTRACT" STATUS
        if (!is_subclass_of($this, 'View')) trigger_error('Class "' . 
get_class($this) . '" is not a subclass of Object', E_USER_ERROR);
        return null;
    }

    //------------------------------ --* GETTER/SETTER METHODS *-- 
-----------------------------------------

    /**
     * @abstract
     * @param int id
     * @param mixed $imageLocationPath (optional)
     */
    function getAssociationDisplay($id, $imageLocationPath = '') {}

    /**
     * @abstract
     * @param mixed $section
     * @param int id (optional)
     */
    function getAssocSectionsArray($associatedSection, $id = '') {}

    /**
     * @param object $result
     * @return int total items in $result
     */
    function &getCount($result) {
        return @sizeof($result);
    }
    //------------------------------ --* END OF GETTER/SETTER METHODS 
*-- -------------------------------

    /**
     * @abstract
     * @param int id (optional)
     */
    function assocSectionsDisplayHTML($id = '') {}
   
    /**
     * @abstract
     */
    // DISPLAY HTML
    function displayHTML() {}

    /**
     * @abstract
     */
    // DISPLAY PAGE LINKS
    function displayPage() {}

    /**
     * @abstract
     */
    // DISPLAY TEXT
    function displayText() {}

    /**
     * @abstract
     */
    // DISPLAY TREE
    function displayTree() {}

    /**
     * @abstract
     */
    // DISPLAY XML
    function displayXML() {}

    // MORE TO COME...

}

/**
 * PaginationView will handle all displays regarding pagination along 
with limit calculations. Class is a subclass of client-scope
 * class View for displayPage() method inheritance
 *
 * @author Phil Powell
 * @version 1.0.0
 * @package IMAGE_CATALOG
 * @see View
  */

class PaginationView extends View {

    /**
     *
      * Result set
     *
     * @access private
     * @param object $result
     */
    var $result;

    /**
     * Constructor
     *
     * @access public
     */
    function PaginationView($result) {                    // CONSTRUCTOR
        $this->result = $result;
    }

    //--------------------------------------- --* GETTER/SETTER METHODS 
*-- -----------------------------------------------------------------

    /**
     * Retrieve "cached" results from $_SESSION variable
     *
      * @access protected
     * @return object $result unserialized collection array
     */
    function &getCachedResult() {                        // STATIC 
OBJECT METHOD
        global $willPaginate, $displayItemLimit;
        foreach ($_REQUEST as $key => $val) if (!isset(${$key})) ${$key} 
= $val;
        if ($willPaginate && (int)$displayItemLimit > 0 && 
$willKeepPageSession) return unserialize($_SESSION['result']);
    }
    //--------------------------------------- --* END OF GETTER/SETTER 
METHODS *-- 
-----------------------------------------------------------------

    /**
     * Do a "faux" caching by placing the entire resultset into a 
$_SESSION variable.  Serialize as this might be an array of objects
     *
     * @access protected
     */
    function cacheResult() {                            // VOID METHOD
        global $willPaginate, $displayItemLimit;
        foreach ($_REQUEST as $key => $val) if (!isset(${$key})) ${$key} 
= $val;
        if ($willPaginate && (int)$displayItemLimit > 0 && 
!$willKeepPageSession && !$page) {
         unset($_SESSION['result']);
         $_SESSION['result'] = serialize($this->result);
         flush();
         ob_flush();
         clearstatcache();
        }
    }

    /**
     * Display pagination links
     *
     * @access public
     * @return mixed HTML
     */
    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;"; else $html .= 
"<a href=\"index.php?$qs&page=$i\">$i</a>&nbsp;";
          if ($i <= $numPages && @sizeof($this->result) % 
(int)$displayItemLimit !== $numPages) $html .= '<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
         $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>";
         }

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

    /**
     * Perform "cache flush" by destroying the session variable 
containing the $result
     *
     * @access protected
     */
    function &flushResult() {                        // STATIC VOID METHOD
        if ($_SESSION['result']) unset($_SESSION['result']);
        flush();
        ob_flush();
        clearstatcache();
    }

    /**
     * Will perform specific array_slice() function on parameter $result 
(IMPORTANT! DO NOT USE $this->result WILL AFFECT PAGINATION!)
     *
     * @access protected
     * @param object $result (reference)
     */
    function &limitResult(&$result) {                // STATIC VOID METHOD
        global $willPaginate, $displayItemLimit;
        foreach ($_GET as $key => $val) if (!isset(${$key})) ${$key} = $val;
        if ($willPaginate && (int)$displayItemLimit > 0 && 
is_array($result) && @sizeof($result) >= $displayItemLimit) {
         // CALCULATE $offset AND $limit
         if ((int)$page === 0) $page = 1;
         $page--;
         $offset = (int)$page * (int)$displayItemLimit;
         $length = ($offset + (int)$displayItemLimit > @sizeof($result)) 
? $offset : (int)$displayItemLimit;
         $result = array_slice($result, $offset, $length);
        }
    }

}
[/PHP]

This is per Joe Crawford's request/**
 * PaginationView will handle all displays regarding pagination along 
with limit calculations. Class is a subclass of client-scope
 * class View for displayPage() method inheritance
 *
 * @author Phil Powell
 * @version 1.0.0
 * @package IMAGE_CATALOG
 * @see View
  */

class PaginationView extends View {

    /**
     *
      * Result set
     *
     * @access private
     * @param object $result
     */
    var $result;

    /**
     * Constructor
     *
     * @access public
     */
    function PaginationView($result) {                    // CONSTRUCTOR
        $this->result = $result;
    }

    //--------------------------------------- --* GETTER/SETTER METHODS 
*-- -----------------------------------------------------------------

    /**
     * Retrieve "cached" results from $_SESSION variable
     *
      * @access protected
     * @return object $result unserialized collection array
     */
    function &getCachedResult() {                        // STATIC 
OBJECT METHOD
        global $willPaginate, $displayItemLimit;
        foreach ($_REQUEST as $key => $val) if (!isset(${$key})) ${$key} 
= $val;
        if ($willPaginate && (int)$displayItemLimit > 0 && 
$willKeepPageSession) return unserialize($_SESSION['result']);
    }
    //--------------------------------------- --* END OF GETTER/SETTER 
METHODS *-- 
-----------------------------------------------------------------

    /**
     * Do a "faux" caching by placing the entire resultset into a 
$_SESSION variable.  Serialize as this might be an array of objects
     *
     * @access protected
     */
    function cacheResult() {                            // VOID METHOD
        global $willPaginate, $displayItemLimit;
        foreach ($_REQUEST as $key => $val) if (!isset(${$key})) ${$key} 
= $val;
        if ($willPaginate && (int)$displayItemLimit > 0 && 
!$willKeepPageSession && !$page) {
         unset($_SESSION['result']);
         $_SESSION['result'] = serialize($this->result);
         flush();
         ob_flush();
         clearstatcache();
        }
    }

    /**
     * Display pagination links
     *
     * @access public
     * @return mixed HTML
     */
    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++) {
          print_r("i = $i<br>");
          if ((int)$i === (int)$page) $html .= "$i&nbsp;"; else $html .= 
"<a href=\"index.php?$qs&page=$i\">$i</a>&nbsp;";
          if ($i <= $numPages && @sizeof($this->result) % 
(int)$displayItemLimit !== $numPages) $html .= '<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
         $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>";
         }

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

    /**
     * Perform "cache flush" by destroying the session variable 
containing the $result
     *
     * @access protected
     */
    function &flushResult() {                        // STATIC VOID METHOD
        if ($_SESSION['result']) unset($_SESSION['result']);
        flush();
        ob_flush();
        clearstatcache();
    }

    /**
     * Will perform specific array_slice() function on parameter $result 
(IMPORTANT! DO NOT USE $this->result WILL AFFECT PAGINATION!)
     *
     * @access protected
     * @param object $result (reference)
     */
    function &limitResult(&$result) {                // STATIC VOID METHOD
        global $willPaginate, $displayItemLimit;
        foreach ($_GET as $key => $val) if (!isset(${$key})) ${$key} = $val;
        if ($willPaginate && (int)$displayItemLimit > 0 && 
is_array($result) && @sizeof($result) >= $displayItemLimit) {
         // CALCULATE $offset AND $limit
         if ((int)$page === 0) $page = 1;
         $page--;
         $offset = (int)$page * (int)$displayItemLimit;
         $length = ($offset + (int)$displayItemLimit > @sizeof($result)) 
? $offset : (int)$displayItemLimit;
         $result = array_slice($result, $offset, $length);
        }
    }

}
[/PHP]



More information about the talk mailing list