Sollte so ziemlich einfach sein das Ding einzubauen (was der Sinn des Ganzen war =)
Nicht von der Methode calc() verwirren lassen. Die ist "uninteressant".
Ich bin zwar immer der Meinung, dass man den Code verstehen sollte den man nutzt,
wenn ich aber jetzt anfange die calc() zu erklären, ... das will keiner lesen.
Sinn:
Einfach nur beim erstellen eines Objekts die wichtigsten Daten rein schieben,
anhand der public Methoden die reinen Zahlen (als String) holen,
und anhand dieser dann Buttons oder Links erstellen.
EDIT 31 08 15: updated:
Klasse umbenannt von PageNav in Pagination ,
(hoffentlich) verständlichere Kommentare hinzugefügt,
und um zwei Methods erweitert: getQueryOffset() und getQueryLimit()
Test: Pagination Test
Kompletter Beispielcode:
<?php
class Pagination
{
protected
$page_offset_current, // (double) : f.e. $_GET['page']
$lines_count_all, // (double) : "SELECT COUNT(*) FROM tbl WHERE ..."
$lines_per_page, // (int) : how many lines per page are viewed
$n, // (int) : nav: Nx[link/button] [{page_offset_current}] Nx[link/button]
$last_page_lines_count, // (int) : how many lines are viewed pm last page (min 1, up to max {lines_per_page})
$pages_max_num, // (double) : number of pages we need to view all lines by viewing {lines_per_page} a page
$n_left, // (double) : nav: Nx[link/button] [{page_offset_current}] ...
$n_right, // (double) : nav: ... [{page_offset_current}] Nx[link/button]
$page_previous, // (double) : nav: [page_previous] Nx[link/button] [{page_offset_current}] ...
$page_next, // (double) : nav: ... [{page_offset_current}] Nx[link/button] [page_next]
$page_jump_backwards, // (double) : nav: [<<] [page_previous] Nx[link/button] [{page_offset_current}] ...
$page_jump_forwards, // (double) : nav: ... [{page_offset_current}] Nx[link/button] [page_next] [>>]
$left_side_nav_num, // (array) : contains left side nav numbers
$right_side_nav_num; // (array) : contains right side nav numbers
public function __construct($page_offset_current, $lines_count_all, $lines_per_page=25, $n=5)
{
$this->page_offset_current = (!$page_offset_current) ? 1 : (double)($page_offset_current);
$this->lines_count_all = (double)$lines_count_all;
$this->lines_per_page = (int)$lines_per_page;
$this->n = (int)$n;
$this->calc();
return;
}
public function __destruct()
{
return;
}
final protected function calc()
{
$this->pages_max_num = floor($this->lines_count_all/$this->lines_per_page);
(($this->last_page_lines_count=$this->lines_count_all-$this->pages_max_num*$this->lines_per_page) < 1)
or (++$this->pages_max_num);
($this->page_offset_current <= $this->pages_max_num)
or ($this->page_offset_current=$this->pages_max_num);
($this->page_offset_current >= 1)
or ($this->page_offset_current=1);
($this->last_page_lines_count > 0)
or ($this->last_page_lines_count=$this->lines_per_page);
(($this->n_left=$this->page_offset_current-$this->n) > 0)
or ($this->n_left=1);
(($this->n_right=($this->n_left === 1) ? $this->n*2+1 : $this->page_offset_current+$this->n) < $this->pages_max_num)
or (($this->n_right=$this->pages_max_num)and ($this->n_left-=$this->n-($this->pages_max_num-$this->page_offset_current)));
($this->n_left > 0)
or ($this->n_left=1);
(($this->page_previous=$this->page_offset_current-1) >= 1)
or ($this->page_previous=1);
(($this->page_next=$this->page_offset_current+1) <= $this->pages_max_num)
or ($this->page_next=$this->pages_max_num);
for(
$i = $this->n_left, $this->left_side_nav_num = array();
$i < $this->page_offset_current;
$this->left_side_nav_num[$i] = $this->f($i),
$i++
);
for(
$i = $this->n_right, $this->right_side_nav_num = array();
$i > $this->page_offset_current;
$this->right_side_nav_num[$i] = $this->f($i),
$i--
);
asort($this->right_side_nav_num);
$page_jump = ($this->pages_max_num/$this->lines_per_page)*$this->n;
( ($this->page_jump_forwards=$this->page_offset_current+$page_jump) < $this->pages_max_num
and $this->pages_max_num-$this->page_jump_forwards > $this->n*2
) or ($this->page_jump_forwards=$this->pages_max_num);
( ($this->page_jump_backwards=$this->page_offset_current-$page_jump) > 1
and $this->page_jump_backwards > $this->n*2
) or ($this->page_jump_backwards=1);
return;
}
final protected function f($n)
{ // returns numeric string
return sprintf("%.0f", $n);
}
final public function getPageFirst()
{ // returns numeric string
/* get first page offset
[first] [<<] [prev] [1] [2] 3 [4] [5] [next] [>>] [last]
^
*/
return '1';
}
final public function getPageLast()
{ // returns numeric string
/* get last page offset
[first] [<<] [prev] [1] [2] 3 [4] [5] [next] [>>] [last]
^
*/
return $this->getPagesMaxNum();
}
final public function getPagePrevious()
{ // returns numeric string
/* get previous page offset
[first] [<<] [prev] [1] [2] 3 [4] [5] [next] [>>] [last]
^
*/
return $this->f($this->page_previous);
}
final public function getPageNext()
{ // returns numeric string
/* get next page offset
[first] [<<] [prev] [1] [2] 3 [4] [5] [next] [>>] [last]
^
*/
return $this->f($this->page_next);
}
final public function getPageJumpBackward()
{ // returns numeric string
/* get page offset to jump backwards a dynamic amount of pages
[first] [<<] [prev] [1] [2] 3 [4] [5] [next] [>>] [last]
^
*/
return $this->f($this->page_jump_backwards);
}
final public function getPageJumpForward()
{ // returns numeric string
/* get page offset to jump forward a dynamic amount of pages
[first] [<<] [prev] [1] [2] 3 [4] [5] [next] [>>] [last]
^
*/
return $this->f($this->page_jump_forwards);
}
final public function getMiddle() // alias for ::getPageCurrent()
{ // returns numeric string
/* get page offset for the current page
[first] [<<] [prev] [1] [2] 3 [4] [5] [next] [>>] [last]
^
*/
return $this->getPageCurrent();
}
final public function getLeft()
{ // returns array
/* get array of page offsets for left side
[first] [<<] [prev] [1] [2] 3 [4] [5] [next] [>>] [last]
^ ^
*/
return $this->left_side_nav_num;
}
final public function getRight()
{ // returns array
/* get array of page offsets for right side
[first] [<<] [prev] [1] [2] 3 [4] [5] [next] [>>] [last]
^ ^
*/
return $this->right_side_nav_num;
}
final public function getPageCurrent()
{ // returns numeric string
/* get page offset for the current page
[first] [<<] [prev] [1] [2] 3 [4] [5] [next] [>>] [last]
^
*/
return $this->f($this->page_offset_current);
}
final public function getPagesMaxNum()
{ // returns numeric string
/* get last page offset
you are on page 3 of total 40000000
^
*/
return $this->f($this->pages_max_num);
}
final public function getLinesCountAll()
{ // returns numeric string
/* get count all lines (data sets) -- the given parameter $lines_count_all at ::__construct()
viewing 25 lines per page (25 lines on the last page) of total 1000000000 lines
^
*/
return $this->f($this->lines_count_all);
}
final public function getLinesPerPage()
{ // returns numeric string
/* get count lines per page -- the given parameter $lines_per_page at ::__construct()
viewing 25 lines per page (25 lines on the last page) of total 1000000000 lines
^
*/
return $this->f($this->lines_per_page);
}
final public function getLinesLastPage()
{ // returns numeric string
/* get count lines for the last page
viewing 25 lines per page (25 lines on the last page) of total 1000000000 lines
^
explanation:
we got 11 lines and viewing 5 lines at one page.
so we got 2 pages with 5 lines, and a last page with 1 line.
[first] [<<] [prev] 1 [2] [3] [next] [>>] [last]
^ viewing 5 lines on that page (1-5)
[first] [<<] [prev] [1] 2 [3] [next] [>>] [last]
^ viewing 5 lines on that page (6-10)
[first] [<<] [prev] [1] [2] 3 [next] [>>] [last]
^ viewing 1 line on that page (11)
*/
return $this->f($this->last_page_lines_count);
}
final public function getQueryOffset()
{ // returns int
/* get current page offset for db query like
SELECT ... FROM ... WHERE ... LIMIT 0, 25;
^
*/
if(($offset=$this->getPageCurrent()-1) > 0){
return $offset*$this->getLinesPerPage();
}
return 0;
}
final public function getQueryLimit()
{ // returns int
/* get limit for db query like
SELECT ... FROM ... WHERE ... LIMIT 0, 25;
^
*/
return $this->getLinesPerPage();
}
/*
explanation/example/overview:
example output:
you are on page 5 of total 40000000
viewing 25 lines per page (25 lines on the last page) of total 1000000000 lines
[first] [<<] [prev] [2] [3] [4] 5 [6] [7] [8] [next] [>>] [last]
placements:
you are on page ::getPageCurrent() of total ::getPagesMaxNum()
viewing ::getLinesPerPage() lines per page (::getLinesLastPage() lines on the last page) of total ::getLinesCountAll() lines
[::getPageFirst()] [::getPageJumpBackward()] [::getPagePrevious()]
foreach ::getLeft()
::getMiddle()
foreach ::getRight()
[::getPageNext()] [::getPageJumpForward()] [::getPageLast()]
*/
/* query example:
SELECT ... FROM ... WHERE ... LIMIT ::getQueryOffset(), ::getQueryLimit();
*/
}
/////////////////////////////
// create a new Pagination //
/////////////////////////////
/* $page_offset_current
in this example we got the current page as GET-parameter "page" -- domain.com?page=1 */
$page_offset_current = isset($_GET['page']) ? $_GET['page'] : 1;
/* $lines_count_all
here you would use your db query like
$lines_count_all = ...("SELECT COUNT(*) FROM tbl WHERE ..."); */
$lines_count_all = 1000000000;
/* $lines_per_page (default 25)
set how many lines (data sets) per page should be viewed at one page */
$lines_per_page = 25;
/* $n (default 5)
how many choices to each direction (left/right)
example $n = 3: [first] [<<] [prev] [1] [2] [3] 4 [5] [6] [7] [next] [>>] [last]
^ ^ ^
3 on left, and 3 on right.*/
$n = 3;
// create new object from Pagination:
$Pagination = new Pagination($page_offset_current, $lines_count_all, $lines_per_page, $n);
/////////////////////////
// an example document //
/////////////////////////
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Pagination Example</title>
<style type="text/css">
.code{
clear:both;
font-size:15px;
color:#666;
white-space:pre;
font-family:monospace;
display:block;
}
</style>
</head>
<body>
<h1>Pagination Example</h1>
<h3>can handle extremely large numbers (f.e. page 111111111111)</h3>
<div>
you are on page <?php echo $Pagination->getPageCurrent(); ?>
of total <?php echo $Pagination->getPagesMaxNum(); ?>
</div>
<div>
viewing <?php echo $Pagination->getLinesPerPage(); ?> lines per page
(<?php echo $Pagination->getLinesLastPage(); ?> lines on the last page)
of total <?php echo $Pagination->getLinesCountAll(); ?> lines
</div>
<form action="" method="GET">
<input type="number" name="page" value="" placeholder="goto page ..." />
<input type="submit" value="go" />
</form>
<div>
<a href="?page=<?php echo $Pagination->getPageFirst(); ?>">[first]</a>
<a href="?page=<?php echo $Pagination->getPageJumpBackward(); ?>">[<<]</a>
<a href="?page=<?php echo $Pagination->getPagePrevious(); ?>">[prev]</a>
<!-- START nav left buttons/links -->
<?php
foreach($Pagination->getLeft() as $number){
?>
<a href="?page=<?php echo $number; ?>">[<?php echo $number; ?>]</a>
<?php
} // /foreach
?>
<!-- END nav left buttons/links -->
<span><?php echo $Pagination->getMiddle(); ?></span> <!-- current page -->
<!-- START nav right buttons/links -->
<?php
foreach($Pagination->getRight() as $number){
?>
<a href="?page=<?php echo $number; ?>">[<?php echo $number; ?>]</a>
<?php
} // /foreach
?>
<!-- END nav right buttons/links -->
<a href="?page=<?php echo $Pagination->getPageNext(); ?>">[next]</a>
<a href="?page=<?php echo $Pagination->getPageJumpForward(); ?>">[>>]</a>
<a href="?page=<?php echo $Pagination->getPageLast(); ?>">[last]</a>
</div>
<br>
<hr>
<h3>An example query to get the current requested data from database:</h3>
<div class="code">SELECT
...
FROM
`db`.`tbl`
WHERE
...
LIMIT $Pagination->getQueryOffset(), $Pagination->getQueryLimit()
;</div>
<h3>for the current page the LIMIT is:</h3>
<div class="code">LIMIT <?php echo $Pagination->getQueryOffset(); ?>, <?php echo $Pagination->getQueryLimit(); ?></div>
<br>
<hr>
<h2>The complete code:</h2>
<div class="code"><?php echo htmlspecialchars(file_get_contents(__FILE__)); ?></div>
</body>
</html>
Alles anzeigen
Und als kleine Übersicht:
$page_offset_current = isset($_GET['page']) ? $_GET['page'] : 1;
$lines_count_all = ...("SELECT COUNT(*) FROM ... WHERE ...;");
$lines_per_page = 25;
$n = 3;
$Pagination = new Pagination($page_offset_current, $lines_count_all, $lines_per_page, $n);
$offset = $Pagination->getQueryOffset(); // get current page offset for db query
$limit = $Pagination->getQueryLimit(); // get limit for db query
$data_sets = ...("SELECT ... FROM ... WHERE ... LIMIT $offset, $limit;");
$Pagination->getPageCurrent() // the current page number we are looking at
$Pagination->getPagesMaxNum() // the total amount of pages we can view
$Pagination->getLinesPerPage() // the amount of lines (data sets) we are viewing at one page
$Pagination->getLinesLastPage() // the amount of lines (data sets) we are viewing at the last page
$Pagination->getLinesCountAll() // the amount of all lines (data sets)
$Pagination->getPageFirst() // get first page offset
$Pagination->getPageJumpBackward() // get page offset to jump backwards a dynamic amount of pages
$Pagination->getPagePrevious() // get previous page offset
$Pagination->getLeft() // get array of page offsets for left side
$Pagination->getMiddle() // get page offset for the current page (alias ::getPageCurrent())
$Pagination->getRight() // get array of page offsets for right side
$Pagination->getPageNext() // get next page offset
$Pagination->getPageJumpForward() // get page offset to jump forward a dynamic amount of pages
$Pagination->getPageLast() // get last page offset
Alles anzeigen