Re: Pager - return array rather than HTML..
| From: | Tomas V.V.Cox | Date: | Thu, 27 Jun 2002 12:58:31 +0000 |
| Subject: | Re: Pager - return array rather than HTML.. | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-7389@lists.php.net to get a copy of this message | ||
Alan Knowles wrote:
>
> Attached (hopefully) is a patch to return an array rather than HTML. I
> need the 'calculations' of pager, but not the HTML output.
Haven't you looked at DB_Pager? It does exactly that (and in the
lastests versions it doesn't need the use of a database).
Tomas V.V.Cox
> It allows you to start it up something like this.
> $pager = new Pager(array(
> 'totalItems' => $N,
> 'perPage' => $options['perPage'],
> 'returnType' => 'RAW',
> 'currentPage' => $currentPage
> ));
>
> list($number_start, $array, $number_end) = $pager->getLinks();
>
> where $array is
> [1] => 0
> [2] => 1
> [3] => 0
> [4] => 0
>
> there is also a small change to urlencode the querystring - which may
> not be neccesary......
>
> regards
> alan
>
> ------------------------------------------------------------------------
> Index: Pager.php
> ===================================================================
> RCS file: /repository/pear/Pager/Pager.php,v
> retrieving revision 1.1
> diff -u -r1.1 Pager.php
> --- Pager.php 9 Feb 2002 22:46:47 -0000 1.1
> +++ Pager.php 27 Jun 2002 12:36:48 -0000
> @@ -22,6 +22,7 @@
> *
> */
>
> +
> class Pager {
>
> /**
> @@ -52,13 +53,19 @@
> * Total number of items in the data
> * @var integer
> */
> - var $_totalItems;
> + var $_totalItems = 0;
>
> /**
> * Page data generated by this class
> * @var array
> */
> var $_pageData;
> + /**
> + * the return Type - default is HTML,
> + * otherwise an you get page numbers returned, and an associative array of pageids =>
> isselected
> + * @var string
> + */
> + var $_returnType = 'HTML';
>
> /**
> * Constructor
> @@ -68,12 +75,14 @@
> * @param $params An associative array of parameters This can contain:
> * currentPage Current Page number (optional)
> * perPage Items per page (optional)
> - * itemData Data to page
> + * itemData Data to page (optional)
> + * or totalItems Total Number of items (optional)
> + * returnType HTML or RAW (optional)
> */
> function pager($params = array())
> {
> global $HTTP_GET_VARS;
> -
> +
> $this->_currentPage = max((int)@$HTTP_GET_VARS['pageID'], 1);
> $this->_perPage = 10;
> $this->_itemData = array();
> @@ -81,8 +90,9 @@
> foreach ($params as $name => $value) {
> $this->{'_' . $name} = $value;
> }
> -
> - $this->_totalItems = count($this->_itemData);
> + if (!$this->_totalItems) {
> + $this->_totalItems = count($this->_itemData);
> + }
> }
>
> /**
> @@ -145,7 +155,7 @@
> $this->_generatePageData();
> }
>
> - if (isset($this->_pageData[$pageid])) {
> + if (isset($this->_totalItems)) {
> return array(($this->_perPage * ($pageid - 1)) + 1, min($this->_totalItems,
> $this->_perPage * $pageid));
> } else {
> return array(0,0);
> @@ -214,6 +224,11 @@
> */
> function _generatePageData()
> {
> + if (!count($this->_itemData)) {
> + $this->_totalPages = ceil((float)$this->_totalItems /
> (float)$this->_perPage);
> + return;
> + }
> +
> $this->_totalItems = count($this->_itemData);
> $this->_totalPages = ceil((float)$this->_totalItems /
> (float)$this->_perPage);
> $i = 1;
> @@ -239,6 +254,7 @@
> global $HTTP_SERVER_VARS;
>
> // Sort out query string to prevent messy urls
> +
> $querystring = array();
> if (!empty($HTTP_SERVER_VARS['QUERY_STRING'])) {
> $qs = explode('&', $HTTP_SERVER_VARS['QUERY_STRING']);
> @@ -252,7 +268,7 @@
> }
>
> foreach ($qs as $name => $value) {
> - $querystring[] = $name . '=' . $value;
> + $querystring[] = $name . '=' . urlencode($value);
> }
>
> return $HTTP_SERVER_VARS['SCRIPT_NAME'] . '?' .
> implode('&', $querystring) . (!empty($querystring) ? '&' : '')
> . 'pageID=';
> @@ -268,13 +284,15 @@
> function _getBackLink($url, $link = '<< Back')
> {
> // Back link
> +
> if ($this->_currentPage > 1) {
> - $back = '<a href="' . $url . ($this->_currentPage - 1) .
> '">' . $link . '</a>';
> - } else {
> - $back = '';
> + if ($this->_returnType == 'HTML') {
> + return '<a href="' . $url . ($this->_currentPage - 1) .
> '">' . $link . '</a>';
> + }
> + return ($this->_currentPage - 1);
> }
> + return '';
>
> - return $back;
> }
>
> /**
> @@ -289,7 +307,12 @@
> $params['itemData'] = range(1, max(1, $this->_totalPages));
> $pager =& new Pager($params);
> $links =
> $pager->getPageData($pager->getPageIdByOffset($this->_currentPage));
> -
> + if ($this->_returnType != 'HTML') {
> + foreach($links as $value) {
> + $ret[$value] = ($this->_currentPage == $value);
> + }
> + return $ret;
> + }
> for ($i=0; $i<count($links); $i++) {
> if ($links[$i] != $this->_currentPage) {
> $links[$i] = '<a href="' . $this->_getLinksUrl() .
> $links[$i] . '">' . $links[$i] . '</a>';
> @@ -309,12 +332,16 @@
> function _getNextLink($url, $link = 'Next >>')
> {
> if ($this->_currentPage < $this->_totalPages) {
> - $next = '<a href="' . $url . ($this->_currentPage + 1) .
> '">' . $link . '</a>';
> - } else {
> - $next = '';
> - }
> + if ($this->_returnType == 'HTML') {
> + return '<a href="' . $url . ($this->_currentPage + 1) .
> '">' . $link . '</a>';
> + }
> + return $this->_currentPage + 1;
> + }
> +
> + return '';
> +
>
> - return $next;
> +
> }
> }
>
>
> ------------------------------------------------------------------------
> --
> PEAR Development Mailing List (http://pear.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php