PHP Classes

mypager.class.php

Recommend this page to a friend!

      MyPager  >  All threads  >  mypager.class.php  >  (Un) Subscribe thread alerts  
Subject:mypager.class.php
Summary:number of numeric links on the left and right from current
Messages:5
Author:Raffi Hovhannesian
Date:2006-02-10 12:58:58
Update:2006-03-30 23:41:52
 

  1. mypager.class.php   Reply   Report abuse  
Picture of Raffi Hovhannesian Raffi Hovhannesian - 2006-02-10 12:58:58
The link_range var is not changing anything when it's default value is changed... as I learned from you class this is supposed to have somethign to do with the number of numeric links on the left and right from the current page's number.

Regards
Hayrenagits

  2. Re: mypager.class.php   Reply   Report abuse  
Picture of Vagharshak Tozalakyan Vagharshak Tozalakyan - 2006-02-13 22:23:14 - In reply to message 1 from Raffi Hovhannesian
:) Just write $pager->links_range instead of $this->links_range. Sorry for a little bug in the sample script.

Here is another example:

<?php

$test_array = array();
$total_num = 230;
for ($i=0; $i<$total_num; $i++)
{
$test_array[$i] = 'Item ' . $i;
}

require_once('mypager.class.php');

$pager = new MyPager(20);
$pager->on_first_last = true;
$pager->on_prev_next = true;
$pager->links_range = 5;
$pager->total = $total_num;

for ($i = $pager->offset; $i < ($pager->offset + $pager->limit) && $i < $total_num; $i++)
{
echo $test_array[$i] . '<br />';
}

list($r1, $r2) = $pager->GetRange();
echo "<p>Display $r1-$r2 of " . $pager->total . '</p>';

$pager->PrintLinks();

?>

  3. Re: mypager.class.php   Reply   Report abuse  
Picture of Chris Smith Chris Smith - 2006-03-26 19:32:17 - In reply to message 1 from Raffi Hovhannesian
First, thanks for this class too ! It's simple, small, it was exactly what I need. Although I have modified it some place:

- line 14: I have added a new property (to get more professional class feeling :) ):
var $links_html = null;
- and line 21: $this->links_html = '';
- and line 74,80,87,92,99,105: change the echo to this:
$this->links_html .= .......
- and line 107: return $this->links_html;
- so in line 38: change the method name to: GetLinks()

There was a problem in the class, when a user change the value of the offset by hand (at the address bar) to a value that is greater then the total: in this case there were no displayable records, because the query is based upon the offset value. That's why I have changed the constructor to this:
- line 16: function MyPager($_total, $limit = 10)
- line 19: change to this: $this->total = $_total;
- line 22-29: It wasn't really a problem the condition (beside of the missing total condition), but I prefer that the user do as little harm as possible, so I have modified the condition to this:
if ( ! isset($_GET['offset'])
|| ! ( (string)(int)$_GET['offset'] === (string)$_GET['offset'] && $_GET['offset'] >= 0 )
|| $this->total-1 < $_GET['offset'] )
$this->offset = 0;
else $this->offset = $_GET['offset'];
the difference between the two, if the value of the offset is:
val 1st 2nd
x5 -> 0 0
5a -> 5 0
-5a -> 5 0
+5a -> 5 0
05 -> 5 0

- at last line 125: to have all values in 1 array:
return array($this->offset + 1, $upper, $this->total);

The sample file is only changed some place. The most important thing is that the query in line18-19 must have executed before the instance of the class is constructed.

If you like these changes, feel free to modify your class. If not, no problem :)

With best regards
Chris

  4. Re: mypager.class.php   Reply   Report abuse  
Picture of Chris Smith Chris Smith - 2006-03-26 19:37:32 - In reply to message 3 from Chris Smith
In the above message at the "- line 16" row, the correct is:
function MyPager($_total = 0, $limit = 10)

  5. Re: mypager.class.php   Reply   Report abuse  
Picture of Chris Smith Chris Smith - 2006-03-30 23:41:52 - In reply to message 4 from Chris Smith
I have made some modifications again (I think it's now perfect for me):

- line 61-69: I have replaced the url rewriting with preg_replace to this (I think it's faster):
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'] . '?' . preg_replace( '/(&offset)=(\w*)/', '', $_SERVER['QUERY_STRING'] ) . '&amp;';

- in those lines, where you have created the links, I have added a 'title' property to the 'a' tag, which shows the number of page and the number of items on it (for this I have created a variable from the offset count, that you have done), eg at the case of the prev link:
if ($current_page != 1 && $this->on_prev_next) {
$_offset = ($current_page - 2) * $this->limit;
$href = $url . 'offset=' . $_offset;
$this->links_html .= "<li><a href=\"$href\" title=\"Prev page: ".($_offset+1)."-".($_offset+$this->limit)."\">&lt;</a></li> \n";
}

- and last I have added a new simple method called GetOffset():
/**
* Returns the offset based upon the rank of the element
*
* @param int $rank the rank of the element
* @return int returns the offset number
*/
function GetOffset(&$rank)
{
return floor($rank / $this->limit) * $this->limit;
}

It can be used to reference to an element by id:
In the main script I have selected the rank value of the desired id (of course, after the examination, that the id is still exist), and this method returns the offset, so I can reference a news item by id from the web (or after editing, or when a news item has deleted I have selected the next available id):
eg. the referece-link:
- http://www.filter.lan/index.php?page=news&p=12
and the main script generated link, with the help of this method, is:
- http://www.filter.lan/index.php?page=news&offset=10#p12

Once again: thanks for this great class too, it has many good ideas !