-
-
// Establish database connection - $link = mysql_connect("localhost", "mysql_user", "mysql_password")
- or die("Could not connect: " . mysql_error ());
- // Get the current page number
- if( isset($_GET['page']) ){
- $page = intval( $_GET['page'] );
- }
- else{
- $page = 1;
- }
- //Number of pages per page
- $PageSize = 10;
- //Get the total amount of data
- $sql = "select count(*) as amount from table";
- $result = mysql_query($sql);
- $row = mysql_fetch_row($result);
- $amount = $row['amount'];
- // Calculate how many pages there are in total
- if( $amount ){
- if( $amount < $page_size ){ $page_count = 1; } //If the total data amount is less than $PageSize, then there is only one page
- if( $amount % $page_size ){ //Take the remainder of the total data amount divided by the number of pages per page
- $page_count = (int)($ amount / $page_size) + 1; //If there is a remainder, the number of pages is equal to the total data amount divided by the number of pages, rounded up and plus one
- }else{
- $page_count = $amount / $page_size; //If If there is no remainder, the number of pages is equal to the total data amount divided by the number of each page
- }
- }
- else{
- $page_count = 0;
- }
// Page turning link
- $page_string = '';
- if( $page == 1 ){
- $page_string .= 'First page|Previous page|';
- }
- else{
- $page_string .= '|Previous page|';
- }
- if( ($page == $page_count) || ($page_count == 0) ){
- $page_string .= 'next page|last page';
- }
- else{
- $page_string .= 'Next page|Last page ';
- }
- // Get data and return the result 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();
- }
- // It does not include the code to display the results, so that is beyond the scope of discussion. You can simply use the obtained two-dimensional array to display the results using foreach
- ?>
Copy code 4. OO style paging code
The following database connections are handled using the pear db class.
- // FileName: Pager.class.php
- // Paging class, this class is only used to process data structures and is not responsible for processing display work
- Class Pager
- {
- var $PageSize ; //Number of each page
- var $CurrentPageID; //Current number of pages
- var $NextPageID; //Next page
- var $PreviousPageID; //Previous page
- var $numPages; //Total number of pages
- var $numItems; //Total number of records
- var $isFirstPage; //Whether it is the first page
- var $isLastPage; //Whether it is the last page
- var $sql; //SQL query statement
-
- function Pager($option)
- {
- global $db;
- $this->_setOptions($option);
- // Total number of items
- if ( !isset($this->numItems) )
- {
- $res = $db->query( $this->sql);
- $this->numItems = $res->numRows();
- }
- // Total number of 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;
- }
-
- /***
- *
- * Return the database connection of the result set
- * When the result set is relatively large, you can directly use this method to obtain the database connection, and then traverse outside the class, so the overhead is smaller
- * If the result set is not very large, you can Directly use getPageData to obtain the result in two-dimensional array format
- * The getPageData method also calls this method to obtain 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); //Use Pear DB::limitQuery method to ensure Database compatibility
-
- return $link;
- }
- else
- {
- return false;
- }
- }
-
- /***
- *
- * Return the result set in the format of a two-dimensional array
- *
- ***/
-
- 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
2. Test php paging code
-
- // FileName: test_pager.php
- // This is a simple sample code, the code for using the pear db class to establish a database connection is omitted
- 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 = "Homepage|Previous page|";
- }
- else
- {
- $turnover = "Homepage|PreviousPageID."&numItems=" .$pager->numItems."'>Previous Page|";
- }
- if ( $pager->isLastPage )
- {
- $turnover .= "Next Page|Last Page" ;
- }
- else
- {
- $turnover .= "NextPageID."&numItems=".$pager->numItems."'>Next Page|numPages."&numItems=".$pager->numItems."'>Last Page" ;
- }
- ?>
Copy code
Instructions:
This class only processes data and is not responsible for display, because it is a bit reluctant to put both data processing and result display into one class.
The display situation and requirements are changeable. It is better to handle it according to the results given by the class. According to this Pager class, inherit a subclass of its own to display different paginations. For example, displaying the user pagination list can:
-
- Class MemberPager extends Pager
- {
- function showMemberList()
- {
- global $db;
-
- $data = $this->getPageData();
- //Code to display results
- // ......
- }
- }
- /// Call
- if ( isset($_GET['page']) )
- {
- $page = (int)$_GET['page'];
- }
- else
- {
- $page = 1;
- }
- $sql = "select * from members order by id";
- $pager_option = array(
- "sql" => $sql,
- "PageSize" => 10,
- "CurrentPageID" => $page
- );
- if ( isset($_GET['numItems']) )
- {
- $pager_option['numItems'] = (int)$_GET['numItems'];
- }
- $pager = @new MemberPager($pager_option);
- $pager->showMemberList();
- ?>
Copy code
Compatibility of the second different database, in different databases The way to write a section of intercepted results is different.
mysql: select * from table limit offset, rows
pgsql: select * from table limit m offset n
...
You need to use the limitQuery method of the pear db class when getting results in the class.
|