PHP paging function [Classic]

Source: Internet
Author: User


<?php
/*
Paging class
Design by Xqbar
qq:174171262
Email:wxddong@163.com
Blog: http://www.xqbar.com
Consider input parameters
Size (number of records per page)
URL (connection address)
Style (pagination style)
SQL (Run statement)
*/
Class page{
Public $sql = ';//Paging SQL statement
public $url = ';//page pass over the next page address, you can add additional parameters to search for use
Public $size =10;//default per page display data 10 records
Public $style =1;//Default page-display style
Private $totalsize Total =0;//data
Private $totalpage Total Pages =0;//
Private $page =1;//Current page
private $link;//Database connection points
Private $flag =false;//Initialization flag Point
Private $offset =0;//Paging offset value limit $offset, $size;
Constructors pass a database connection point
function __construct ($link) {
$this->page= (isset ($_get[' page ')) &&!empty ($_get[' page ']) && (int) ($_get[' page '))? (int) $_get[' page ']:1;//gets the current page parameter value to determine the current page
$this->link= $link;
}
destructor
function __destruct () {
@mysql_close ($this->link);
$this->link=null;
}
Constructors Combine &LT;PHP5
Function page ($link) {
$this->__construct ($link);
}
Class
function init () {
if (Empty ($this->sql)) {$this->halt ("Error warning: <font color= ' red ' >sql statement cannot be empty!") </font> "); exit ();} Determine if SQL exists
$this->sql=str_replace (SQL);//"> ' @_ ', Db_prefix, $this->sql);//replace table prefix in SQL
$this->size= ($this->size<=0): $this->size;//The initial number of data bars per page
$this->page= ($this->page<1)? 1: $this->page;//Current page number value
#大于第一页后优化查询总记录数
Here I used the first time to browse the page when the query database total number of databases, click the next page when passing and no longer query the database, the amount of data may be saved (I think)
if ($this->page==1&&!isset ($_get[' tsize ')) {
SQL "> $result = @mysql_query ($this->sql);
$this->totalsize= @mysql_num_rows ($result);
@mysql_free_result ($result);
}else{
The only disadvantage of the above problem is that the user manually changes the Tsize value in the Address bar and the paging is no longer correct
$this->totalsize= (int) $_get[' tsize '];
}
$this->url= (Empty ($this->url))? $_server['PHP_self ']. Tsize= '. $this->totalsize: $this->url. ' &tsize= '. $this->totalsize;//pass totalsize to the next page
$this->totalpage=ceil ($this->totalsize/$this->size);//Get total number of pages
$this->page= ($this->page> $this->totalpage)? $this->totalpage: $this->page;
$this->offset= ($this->page-1) * $this->size;//Get offset value
}
Query database returns results to two-dimensional arrays
function Fetch_array ($result _type=mysql_both) {
if (! $this->flag) {$this->init (); $this->flag=true}
if ($this->totalsize<=0) {return NULL;}
$this->sql.= "Limit $this->offset, $this->size";
SQL, $this->link "> $result = @mysql_query ($this->sql, $this->link);
while ($row =mysql_fetch_array ($result, $result _type)) {$rows []= $row;}
@mysql_free_result ($result);
return $rows;
}
Get total number of data
function GetSize () {
if (! $this->flag) {$this-init (); $this->flag=true} To determine if it has been initialized before, then return directly (below)
return $this->totalsize;
}
Get Total Pages
function GetPage () {
if (! $this->flag) {$this-init (); $this->flag=true}
return $this->totalpage;
}
Output page links based on incoming parameters
function Outpage () {
if (! $this->flag) {$this-init (); $this->flag=true}
Switch ($this->style) {
#1 x page Total x page First previous next last X piece/page Total x page
Case 1:
$pagehtml = "<span class= ' page ' > <i>{$this->page}</i> page Total <i>{$this->totalpage}</i > page ";
$pagehtml. = ($this->page==1)? "First prev": "<a href= ' {$this->url}&page=1 ' > Home </a> <a href= ' $ This->url}&page= ". ($this->page-1). "' > Prev </a> ";
$pagehtml. = ($this->page== $this->totalpage)? "Next Last": "<a href= ' {$this->url}&page=". ($this->page+1). "' > Next </a> <a href= ' {$this->url}&page={$this->totalpage} ' > Last </a> ';
$pagehtml. = "Altogether <i>{$this->totalsize}</i> strip </span>";
Break
#1 Home Previous Next last X piece/page Total x page
Case 2:
$pagehtml = "<span class= ' page ' >";
$pagehtml. = ($this->page==1)? "First prev": "<a href= ' {$this->url}&page=1 ' > Home </a> <a href= ' $ This->url}&page= ". ($this->page-1). "' > Prev </a> ";
$pagehtml. = ($this->page== $this->totalpage)? "Next Last": "<a href= ' {$this->url}&page=". ($this->page+1). "' > Next </a> <a href= ' {$this->url}&page={$this->totalpage} ' > Last </a> ';
$pagehtml. = "Altogether <i>{$this->totalsize}</i> strip </span>";
Break
#2 1 2 3 4 5
Case 3:
$offset = (ceil ($this-&GT;PAGE/10)-1) *10;
$pagehtml = "<span class= ' page ' >";
if ($offset!=0) {$pagehtml. = "<a href= ' {$this->url}&page=1 ' >|<<</a> <a href= ' {$this-> Url}&page= ". ($this->page-1). "' ><<</a> ";}
For ($i =1 $i <=10; $i + +) {
$n = $i + $offset;
if ($n > $this->totalpage) {break;}
if ($n = = $this->page) {$pagehtml. = "<a href= ' {$this->url}&page= $n ' ><i> $n </i></a>" ;}
else{$pagehtml. = "<a href= ' {$this->url}&page= $n ' ><b> $n </b></a>";}
}
if ($n < $this->totalpage) {$pagehtml. = "<a href= ' {$this->url}&page=." ( $this->page+1). "' >>></a> <a href= ' {$this->url}&page= $this->totalsize ' >>>|</a> ';}
Unset ($offset, $i, $n);
Break
#1
Default
$pagehtml = "{$this->page} page Total {$this->totalpage} page";
$pagehtml. = ($this->page==1)? "First prev": "<a href= ' {$this->url}&page=1 ' > Home </a> <a href= ' $ This->url}&page= ". ($this->page-1). "' > Prev </a> ";
$pagehtml. = ($this->page== $this->totalpage)? "Next Last": "<a href= ' {$this->url}&page=". ($this->page+1). "' > Next </a> <a href= ' {$this->url}&page={$this->totalpage} ' > Last </a> ';
Break
}
return $pagehtml;
}
Error output
function Halt ($msg) {
echo "
echo "
echo "<meta http-equiv= ' content-type ' content= ' text/html; Charset=utf-8 ' >\r\n ';
echo "<title>Error</title>";
echo "
echo "<body><p style= ' line-helght:150%;font-size:10pt ' >";
Echo $msg;
echo "</p></body>";
echo "
}
}
?>

<?php
require_once (' mysql.class.php ');
require_once (' page.class.php ');
$ Db=new MySQL ();
$link = $db->open ();
#定义sql和url
$sql = ' select * from @_info ';(@_ is my table definition prefix is replaced after incoming class)
$url = ';
$mypage =new page ($link);
$ mypage->size=10;
$mypage->style=3;
$mypage->url= $url;
$mypage->sql= $sql. "ORDER BY id DESC";
$ls = $mypage->fetch_array (MYSQL_ASSOC);//Get returned results
if (count ($ls) ==0) {
   echo ' no results ';
} else{
foreach ($ls as $value) {
    echo $value [' title '];
}
//page numbering
Echo $mypage->outpage ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.