paginton-von-cotton.php


Quell Code


<?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; // wenn kkeine page angegeben ist wird bei Page ? gesartet
 
$lines_count_all = 9999;  //alle verfügbare daten aus datenbank angeben mit count() und select aus datenbank holen ("SELECT COUNT(*) FROM tbl WHERE ...")
 
$lines_per_page = 25;
$n = 6; // wie viele Zahlen sollen links und rects neben der mitte sichtbar sein 
 
if($lines_count_all > $page_offset_current){
    $Pagination = new Pagination($page_offset_current, $lines_count_all, $lines_per_page, $n);
}else{
    echo "Deine Seiten auswahl ist größer als wie Daten in der Datenbank";
    return;
}
 
?>
<!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;
		}
        .pagni{
     display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: space-around;
    align-content: space-around;
    align-items: flex-start;
        border:2px solid black;
        padding:5px;
        }
        .buttons{
        border:1px solid black;
        color:white;
        background:black;
        }
        .buttons:hover{
        background:red;
        color:green;
        font-weight:900;
        border:2px black;
        box-shadow:2px 2px 2px 2px green;
        }
        .next,.weiter{
        border:2px solid green;
        background:orange;
        color:black;
        font-weight:600;
        }
        .active{
        border:3px solid green;
        background:green;
        color:white;
        }
        .pfeil{
        color:#abcdef;
        font-weight:500;
        border:2px solid yellow;
        }
	</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" max="<?php echo $Pagination->getLinesCountAll(); ?>" value="" placeholder="goto page ..." />
		<input type="submit" value="go" />
	</form>
	<div class="pagni">
		<a class="buttons first" href="?page=<?php echo $Pagination->getPageFirst(); ?>">[first]</a>
		<a class="buttons pfeil" href="?page=<?php echo $Pagination->getPageJumpBackward(); ?>">[&lt;&lt;]</a>
		<a class="buttons weiter" href="?page=<?php echo $Pagination->getPagePrevious(); ?>">[prev]</a>
		<!-- START nav left buttons/links -->
<?php
foreach($Pagination->getLeft() as $number){
?>
		<a class="buttons left" href="?page=<?php echo $number; ?>">[<?php echo $number; ?>]</a>
<?php
} // /foreach
?>
		<!-- END nav left buttons/links -->
		<span class="buttons active"><?php echo $Pagination->getMiddle(); ?></span> <!-- current page -->
		<!-- START nav right buttons/links -->
<?php
foreach($Pagination->getRight() as $number){
?>
		<a class="buttons right" href="?page=<?php echo $number; ?>">[<?php echo $number; ?>]</a>
<?php
} // /foreach
?>
		<!-- END nav right buttons/links -->
		<a class="buttons next" href="?page=<?php echo $Pagination->getPageNext(); ?>">[next]</a>
		<a class="buttons pfeil" href="?page=<?php echo $Pagination->getPageJumpForward(); ?>">[&gt;&gt;]</a>
		<a class="buttons last" 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>