定製PHP分頁類

這個分頁類完全是根據我的需要寫的,但可以稍加修改變成一個通用的,在處理顯示數據時完全可以引入smarty模板,根據傳入的數據類型,進行相應的顯示,當然還可以印日一個參數指定顯示的類型,比如傳入ul,table,更或者border

傳入的參數是原數據(以數組的形式)和需要分頁的URL($_SERVER['PHP_SELF'])

<?php
/*唯一的參數是控制器傳過來的數據,數據的形式以數據傳遞,可以作爲一個通用分頁類*/
class Rollpage
{
	//分成多少頁
	private $page_total;
	//分頁大小
	private $page_size = 15;
	//被分頁的頁面地址
	private $page_link;
	//當前頁
	private $page_current ;
	//需要被分頁的數據,這裏是對數組進行分頁
	private $words;
	//整體翻頁大小
	private $rollpage_size=10;
	
	//首頁
	private $home;
	//尾頁
	private $end;
	//共有多少頁
	private $total;
	
	private $link='';
	private $pos;
	private $nagv;
	private $table='';
	public function __construct($words, $href)
	{
		$len = count($words);
		$this->words=$words;
		$this->page_total = ($len > 0)? ceil($len/$this->page_size):0;
		$this->page_link=$href;
		//if ...else 語句進行邊界檢測
		$this->check();
	}
	private function check()
	{
		if(!isset($_GET['offset']))
		{
			$this->page_current=1;
		}
		else
		{
			$offset = $_GET['offset'];
			if($offset > $this->page_total)
			{
				$this->page_current = $this->page_total;
			}
			else if($offset < 1)
			{
				$this->page_current = 1;
			}
			else
			{
				$this->page_current = $offset;
			}
		}
	}
	
	//分頁數據
	private function data()
	{
		return array_chunk($this->words, $this->page_size)[$this->page_current-1];
	}
	//首頁
	private function home()
	{
		$this->home = "<a href =".$this->page_link."?offset=1 style='text-decoration:none ;'>首頁</a> ";
	}
	//尾頁
	private function end()
	{
		$this->end = "<a href ='".$this->page_link."?offset=".$this->page_total." '  style='text-decoration:none ;' >尾頁</a>   ";
	}
	//總頁數
	private function total()
	{
		$this->total="共有<span style='color:#eeabf0;'>".$this->page_total."</span>頁";
	}
	//創建分頁鏈接,當前頁禁用<a>..</a>並突出顯示
	private function link()
	{
		if($this->page_total > $this->rollpage_size)
		{
			//根據分頁大小,當分頁總數大於10頁時,每次顯示10頁內容
			$start = floor(($this->page_current-1)/$this->rollpage_size)*$this->rollpage_size+1;
			$end = $start+$this->rollpage_size;
			//整體向上翻10頁
			if($this->page_current > $this->rollpage_size)
			{
				$offset = $start-1;
				$this->link .= "<a href='{$this->page_link}"."?offset={$offset}' "." style=\"text-decoration:none\" > ".'<<<'."<a> ";
			}
			//整體翻十頁關鍵代碼
			for($start; $start < $end; $start++)
			{
				if($start < $this->page_total)
				{
					if($this->page_current == $start)
					{
						$this->link .= "<span style='text-decoration:none; font-size: 19px; color: white;' >".$start."</span> ";
					}
					else
					{
						$this->link .= "<a href='{$this->page_link}"."?offset={$start}' "." style=\"text-decoration:none\" >".$start."<a> ";
					}
				}
				
			}
			//根據邊界條件,顯示上一頁
			if($this->page_current > 1)
			{
				$next_page = $this->page_current-1;
				$this->link .= "<a href='{$this->page_link}"."?offset={$next_page}' "." style=\"text-decoration:none\" > ".'上一頁'."<a> ";
			}
			if($this->page_current < $this->page_total)
			{
				$next_page = $this->page_current+1;
				$this->link .= "<a href='{$this->page_link}"."?offset={$next_page}' "." style=\"text-decoration:none\" > ".'下一頁'."<a> ";
			}
			//整體向下翻10頁
			if($end < $this->page_total)
			{
				$this->link .= "<a href='{$this->page_link}"."?offset={$end}' "." style=\"text-decoration:none\" > ".'>>>'."<a> ";
			}
		}
		//根據分頁大小,當分頁總數小於10頁時,顯示基本分頁信息
		else
		{
			for($i=1; $i <= $this->page_total;$i++)
			{
				//當前頁時,突出顯示
				if($this->page_current == $i)
				{
					$this->link .= "<span style='text-decoration:none; font-size: 19px; color: white;' >".$i."</span> ";
				}
				else
				{
					$this->link .= "<a href='{$this->page_link}"."?offset=$i' "." style=\"text-decoration:none\" >".$i."<a> ";
				}
			}
		}
	}
	//添加一個跳轉表單
	private function pos()
	{
		$this->pos = "<form  action='{$this->page_link}' method='get' style='display: inline;'><input type='text' name='offset' style='width:40px;' > ".
		"<input type='submit' value='跳轉' id='submit' style = 'width: 35px; height: 20px ; margin-top: 2px ; padding:1px;' ></form> ";
	}
	//分頁信息的頭部
	private function header()
	{
		$this->table .= "<div style='margin-left: 400px; margin-top: -18px;'><span style='width: 120px; height:30px; background-color: #a00000; color: #00a000; font-size: 18px;'>查詢歷史</span><span style='width: 120px; height:30px;  margin-left: 10px ;background-color: #a00000;  font-size: 18px;'><a href='./review/reviewWord.html' target='_blank'  style='text-decoration:underline;color: #00a000; '>複習單詞</a></span></div>";
		
		$this->table .= "<table style=' width: 720px;  margin-left: 400px; border: 1px solid gray; padding-left: 80px;'>";
	}
	//分頁主體
	private function table()
	{
		$this->header();
		$word = $this->data();
		$len = count($word);
		for($i=0; $i < $len; $i = $i+3)
		{
			$one = isset($word[$i]) ? $word[$i] : "";
			$two = isset($word[$i+1]) ? $word[$i+1] : "";
			$three = isset($word[$i+2]) ? $word[$i+2] : "";
			$this->table .= "<tr>";
			$this->table .= "<td>".$one."</td>"."<td>".$two."</td>"."<td>".$three."</td>";
			$this->table .= "</tr>";
		}
		$this->table .= "</table>";
		
	}
	//用戶接口,生成分頁信息
	public function getLink()
	{
		$this->home();
		$this->end();
		$this->total();
		$this->link();
		$this->pos();
		$this->table();
		$this->nagv = $this->home.$this->link.$this->end.$this->pos.$this->total;
		echo $this->table;
		echo "<div style='margin-left: 400px; width: 720px; margin-top: 2px;' >".$this->nagv."</div>";;
	}
}
?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章