Establishing a database connection
- $link = mysql_connect ("localhost", "Mysql_user", "Mysql_password")
- Or Die ("Could Not connect:". Mysql_error ());
Get the current number of pages
- if (Isset ($_get[' page ')) {
- $page = intval ($_get[' page ');
- }
- else{
- $page = 1;
- }
Quantity per page
- $PageSize = 10;
Get Total data volume
- $sql = "SELECT count (*) as amount from table";
- $result = mysql_query ($sql);
- $row = Mysql_fetch_row ($result);
- $amount = $row [' Amount '];
Count how many pages there are in total
- if ($amount) {
- if ($amount < $page _size) {$page _count = 1;}//If the total data volume is less than $pagesize, then there is only one page
- if ($amount% $page _size) {//Take the total amount of data divided by the remainder of each page
- $page _count = (int) ($amount/$page _size) + 1; If there is an excess, the number of pages equals the total amount of data divided by the result of each page plus one
- }else{
- $page _count = $amount/$page _size; If there is no remainder, the number of pages equals the total data amount divided by the result of each page
- }
- }
- else{
- $page _count = 0;
- }
Page link
- $page _string = ";
- if ($page = = 1) {
- $page _string. = ' first page | prev | ';
- }
- else{
- $page _string. = ' first page | prev | ';
- }
if ($page = = $page _count) | | ($page _count = = 0)) {
- $page _string. = ' next page | last ';
- }
- else{
- $page _string. = ' next page | last ';
- }
Get data to return results in two-dimensional array format
- if ($amount) {
- $sql = "SELECT * from table ORDER BY id desc limit". ($page-1) * $page _size. ", $page _size";
- $result = mysql_query ($sql);
- while ($row = Mysql_fetch_row ($result)) {
- $rowset [] = $row;
- }
- }else{
- $rowset = Array ();
- }
- There is no code to display the results, that is not the scope of the discussion, as long as a foreach can be used to easily use the resulting two-dimensional array to display the results
- ?>
Copy Code4. oo Style code the database connection in the following code is handled using the Pear DB Class
FileName:Pager.class.php
- Paging class, which is used only to process data structures and is not responsible for working with the display
- Class Pager
- {
- var $PageSize; Number of pages per page
- var $CurrentPageID; Current number of pages
- var $NextPageID; Next page
- var $PreviousPageID; Previous page
- var $numPages; Total pages
- var $numItems; Total Record Count
- var $isFirstPage; Whether the first page
- var $isLastPage; Whether the last page
- var $sql; SQL query Statements
function Pager ($option)
- {
- Global $db;
- $this->_setoptions ($option);
Total number of bars
- if (!isset ($this->numitems))
- {
- $res = $db->query ($this->sql);
- $this->numitems = $res->numrows ();
- }
Total pages
- if ($this->numitems > 0)
- {
- if ($this->numitems < $this->pagesize) {$this->numpages = 1;}
- if ($this->numitems% $this->pagesize)
- {
- $this->numpages= (int) ($this->numitems/$this->pagesize) + 1;
- }
- Else
- {
- $this->numpages = $this->numitems/$this->pagesize;
- }
- }
- Else
- {
- $this->numpages = 0;
- }
Switch ($this->currentpageid)
- {
- Case $this->numpages = = 1:
- $this->isfirstpage = true;
- $this->islastpage = true;
- Break
Case 1:
- $this->isfirstpage = true;
- $this->islastpage = false;
- Break
Case $this->numpages:
- $this->isfirstpage = false;
- $this->islastpage = true;
- Break
Default
- $this->isfirstpage = false;
- $this->islastpage = false;
- }
if ($this->numpages > 1)
- {
- if (! $this->islastpage) {$this->nextpageid = $this->currentpageid + 1;}
- if (! $this->isfirstpage) {$this->previouspageid = $this->currentpageid-1;}
- }
- return true;
- }
/***
- *
- * Returns the database connection for the result set
- * When the result set is relatively large, you can use this method to get the database connection directly, and then traverse outside the class, which is less expensive
- * If the result set is not very large, you can get the result of the two-dimensional array format directly using the Getpagedata method
- * The Getpagedata method also calls this method to get the result.
- *
- ***/
- function Getdatalink ()
- {
- if ($this->numitems)
- {
- Global $db;
- $PageID = $this->currentpageid;
- $from = ($PageID-1) * $this->pagesize;
- $count = $this->pagesize;
- $link = $db->limitquery ($this->sql, $from, $count); Ensuring database compatibility with the Pear Db::limitquery method
- return $link;
- }
- Else
- {
- return false;
- }
- }
/***
- *
- * Returns the result set in a two-dimensional array format
- *
- ***/
- function Getpagedata ()
- {
- if ($this->numitems)
- {
- if ($res = $this->getdatalink ())
- {
- if ($res->numrows ())
- {
- while ($row = $res->fetchrow ())
- {
- $result [] = $row;
- }
- }
- Else
- {
- $result = Array ();
- }
- return $result;
- }
- Else
- {
- return false;
- }
- }
- Else
- {
- return false;
- }
- }
function _setoptions ($option)
- {
- $allow _options = Array (
- ' PageSize ',
- ' Currentpageid ',
- ' SQL ',
- ' NumItems '
- );
foreach ($option as $key = $value)
- {
- if (In_array ($key, $allow _options) && ($value! = null))
- {
- $this $key = $value;
- }
- }
- return true;
- }
- }
- ?>
Copy Code#------------------------
FileName:test_pager.php
- This is a simple example code that omits the code that uses the Pear DB class to establish a database connection
- Require "Pager.class.php";
if (Isset ($_get[' page '))
- {
- $page = (int) $_get[' page ';
- }
- Else
- {
- $page = 1;
- }
$sql = "SELECT * from table order by id";
- $pager _option = Array (
- "SQL" = $sql,
- "PageSize" = 10,
- "Currentpageid" = $page
- );
if (Isset ($_get[' NumItems '))
- {
- $pager _option[' numitems '] = (int) $_get[' NumItems '];
- }
$pager = @new Pager ($pager _option);
- $data = $pager->getpagedata ();
if ($pager->isfirstpage)
- {
- $turnover = "Home Page | prev |";
- }
- Else
- {
- $turnover = "NumItems." > Home | Previouspageid. " &numitems= ". $pager->numitems." > Prev | ";
- }
if ($pager->islastpage)
- {
- $turnover. = "Next page | last";
- }
- Else
- {
- $turnover. = "Nextpageid." &numitems= ". $pager->numitems." > next page |numpages. " &numitems= ". $pager->numitems." > Last ";
- }
- ?>
Copy CodeDescription: This class only handles the data and is not responsible for the display, because I think it is somewhat reluctant to put the data processing and the display of the results into a class. When the situation and requirements are changeable, it is better to treat them according to the results given by the class, and the best way to do this is to inherit a subclass from the pager class to display different pagination, such as displaying a user page list:
-
-
-
!--? php class Memberpager extends Pager
- {
- function sh Owmemberlist ()
- {
- global $db;
- $data = $this->getpagedata ();
- //code to display the result
- //...
- }
- }
-
//Call
- if (isset ($_get[' page '))
- {
- $page = (int) $_get[' page '];
- }
- else
- {
- $page = 1;
- }
-
$sql = "SELECT * from the members order by ID";
- $pager _option = Array (
- "sql" = = $sql,
- "PageSize" = ten,
- "Currentpageid" = $pag e
- );
-
If (isset ($_get[' NumItems '))
- {
- $pager _option[' numitems '] = (int) $_get[' Nu Mitems '];
- }
- $pager = @new Memberpager ($pager _option);
- $pager->showmemberlist ();
- ?
-
Copy CodeThe second area to be explained is the compatibility of different databases, and it is not the same to intercept a result in different databases. Mysql:select * FROM table limit offset, rowspgsql:select * from table limit m offset n ... Therefore, the Limitquery method of the Pear DB Class is required to obtain the result in the class. |