Sie sind hier : sebastian1012.bplaced.net/ homepage-neu / Kurze-Scripte-und-Codeschnipsel / index.php

Zurück


Der Quellcode dazu


>]
		$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;
}
 
?>



	
	Pagination Example
	


	

Pagination Example

can handle extremely large numbers (f.e. page 111111111111)

you are on page getPageCurrent(); ?> of total getPagesMaxNum(); ?>
viewing getLinesPerPage(); ?> lines per page (getLinesLastPage(); ?> lines on the last page) of total getLinesCountAll(); ?> lines
[first] [<<] [prev] getLeft() as $number){ ?> [] getMiddle(); ?> getRight() as $number){ ?> [] [>>] [last]

Add Comment

* Required information
1000
Powered by Commentics

Comments (1)

Gravatar
Admin
Sascha65says...

Die Canvas Balken Uhr ist ja mal Fett