class PageLink {
/* Paging display parameter settings*/
private $db_table = "";
private $db_table_field = "";//Fields in the database to be displayed
private $ condition = ""; //Query conditions
private $sort = ""; //Sort conditions
private $page_size = 0; //Number of records displayed on each page
private $link_num = 0; //Display the number of page number links
private $page = 1; //Current page number
private $records = 0; //Total number of records in the table
private $page_count = 0; //Total number of pages
private $pagestring = ""; //Paging link characters before and after String
private $linkUrl = ""; //Current page path
private $urlPara = ""; //Current page url parameter
private $linkUrlNum = 1;
/*
* Custom paging
*/
private $linkFormat = "";
/* Obtained data*/
private $page_data = "";//Data obtained from the database,
// $page_data is a two-dimensional array received
private $dbHelper;
/* Variable definition part end */
/* Function definition (class method) begin * /
function __construct() {include_once 'DBHelper/DBHelper.php';//This is the database operation class
$this->dbHelper = new DBHelper();
} p>
/*
* Set paging information begin
* @param $db_table table
* @param $db_table_field field
* @param $condition condition
* @param $sort sort
* @param $page_size display Number of items
* @param $link_num number of numeric links
* @param $url page path
* @param $para url parameter
*/
public function set($db_table, $db_table_field, $condition, $sort, $page_size, $link_num, $url, $para) {
$this->db_table = $db_table;//Table name
$this->db_table_field = $db_table_field; //Field array,
$this->condition = $ condition; //Sort condition
$this->sort = $sort; //Sort condition
//The field name to be displayed
//Write into the array
/* db parameter setting end */
/* Paging parameter setting begin */
$this->page_size = $page_size;//The number of records displayed on each page
$this->link_num = $link_num;//Display page turning links Number
$this->linkUrl = $url;
$this->urlPara = $para;
/* Setting paging parameters end */
}
/* Setting paging informationend */
/* Get paging link data begin */
public function get() {
$page_data[0] = $this->pagestring ;
$page_data[1] = $this->page_data;
return $page_data;
}
/* Get pagination link data end */
private function set_page() {
if (isset($_REQUEST["page"])) {
$this->page = intval($ _REQUEST["page"]);
} else {
$this->page = 1;
}
}
/* Page number processing end */
< ;p>/* Get the number of records in db begin */
private function get_records() {
$this->records = $this->dbHelper->counts($ this->db_table, $this->condition);
}
/* Get the number of records in db end */
/* Create Page turning link string begin */
private function page_link() {
$checkPage = intval($this->page / $this->link_num);
$startPage = 1 ;
$stopPage = 1;
if($checkPage == 0 && $this->page < $this->link_num){
$startPage = 1;
}
$linkPage = ($this-> link_num / 2);
if($this->page > $linkPage){
$startPage = $this->page - $linkPage;
}
if(($startPage+$this->link_num)> ;$this->page_count){
$startPage = $this->page_count - $this->link_num + 1;
}
if($startPage<1){ $startPage = 1; } $ stopPage = $startPage+($this->link_num - 1);
if($stopPage> $this->page_count){
$stopPage = $this->page_count;
}
$countStr = "共".$this->records."條記錄";
$currStr = "".$this->page." /".$this->page_count."頁 ";
$beginLink = "首頁 ";
$preLink = "上一頁 ";
$nextLink = "下一頁 ";
$noPreLink = "上一頁 ";
$noNextLink = "下一頁 ";
$endLink = "尾頁 ";
if($this->page > ($linkPage + 1) && $this->page_count > $this->link_num){
$currPage .= "1 ";
}
for($i=$startPage;$i<=$stopPage;$i++){ if($i == $this->page){
$currPage .= "".$i." ";
}
else{
$currPage .= "".$i." ";
}
}
if(($this->page_count - $this->page) > $linkPage && $this->page_count > $this->link_num){
$currPage .= "... ".$this->page_count." ";
}
$jumpPage .= " ";
$jumpPage .= " linkUrl
."?page='+document.getElementById('page_text_".$this->linkUrlNum."').value+'".$this->urlPara
."'" name='page_submit' value='GO' />";
$this->linkUrlNum++;
if(!empty($this->linkFormat)){
$tempLinkFormat = $this->linkFormat;
$tempLinkFormat = str_replace("總記錄", $countStr, $tempLinkFormat);
$tempLinkFormat = str_replace("頁次", $currStr, $tempLinkFormat);
if($this->page_count > 1){
$tempLinkFormat = str_replace("首頁", $beginLink, $tempLinkFormat);
if($this->page > 1){
$tempLinkFormat = str_replace("上一頁", $preLink, $tempLinkFormat);
}
else{
$tempLinkFormat = str_replace("上一頁", $noPreLink, $tempLinkFormat);
}
if($this->page < $this->page_count){
$tempLinkFormat = str_replace("下一頁", $nextLink, $tempLinkFormat);
}
else{
$tempLinkFormat = str_replace("下一頁", $noNextLink, $tempLinkFormat);
}
$tempLinkFormat = str_replace("尾頁", $endLink, $tempLinkFormat);
$tempLinkFormat = str_replace("分頁", $currPage, $tempLinkFormat);
$tempLinkFormat = str_replace("跳轉", $jumpPage, $tempLinkFormat);
}
else{
$tempLinkFormat = str_replace("首頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("上一頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("下一頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("尾頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("分頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("跳轉", "", $tempLinkFormat);
}
}
$this->pagestring.=$countStr." ".$currStr;
if($this->page_count > 1){
$this->pagestring.=$beginLink;
if($this->page > 1){
$this->pagestring.=$preLink;
}
$this->pagestring.=$currPage;
if($this->page < $this->page_count){
$this->pagestring.=$nextLink;
}
$this->pagestring.=$endLink.$jumpPage;
}
}
/* Create page turning link string end */
/* Get data begin */
private function fetch_data() {
if ($this->records) {
$limit = ($this->page - 1) * $this->page_size . ",$this->page_size";
$this ->page_data = $this->dbHelper->fetch($this->db_table, $this->condition, $this->sort, $limit);
}
}
/* Get data end */
/* Create paging begin */
public function create_page() {
$this-> ;set_page();
$this->get_records();
$this->page_count = ceil($this->records / $this->page_size);
$this->page_link();
$this->fetch_data();
}
/* Create pagination end */
function __destruct() {
}
/* Function definition (class method) end */
}
/*
Call method
include_once 'PageLink.php';
$pageLink = new PageLink() ; //Instantiate the object
$pageLink->set("table", "*(or field)", "condition", "sort", "number of data items", "number of links per page", "page( list.php)", "Other parameters except ?page=1 (&id=1&name=test)");//Pass in parameters
$pageLink->create_page();//Create paging
$list = $ pageLink->get();//Get data
echo $list[0];//Page link
print_r($list[1]); //Print out the data, or loop it out as you need
*/
Copy code