- /**
- * PHP Paging code
- * */
- /**
- * Link Database
- * bbs.it-home.org
- * @param string $strHost database server host Address
- * @param string $strAccount Database account
- * @param string $strPassword database Password
- * @return Resource
- * */
- function Mysqlconnect ($strHost, $strAccount, $strPassword, $strDBname)
- {
- $strHost =trim ($strHost);
- $strAcount =trim ($strAccount);
- $strPassword =trim ($strPassword);
- $resLink =mysql_connect ($strHost, $strAccount, $strPassword);
- if (! $resLink)
- {
- return false;
- }
- Else
- {//set names ... Set according to the encoding of the database
- mysql_query (' Set names UTF8 ', $resLink);
- $isValidate =mysql_select_db ($strDBname, $resLink);
- if ($isValidate)
- {
- return $resLink;
- }
- Else
- {
- return false;
- }
- }
- }
- /**
- * Accept the current page number of the page, calculate the corresponding parameter value
- * Includes: Start Page $arrparameter[' start ']
- * End page number $arrparameter[' End ']
- * Total number of records $arrparameter[' all '
- * Number of records displayed per page $arrparameter[' nums ']
- * Number of links displayed on the page $arrparameter[' links '
- * SQL statement to query $arrparameter[' SQL ']
- * Paging bar type $arrparameter[' tag ']
- *
- int $intPage Current page number value
- int $intNums The number of records displayed per page
- int $intLinks The number of links displayed in the page
- String $strTablename pagination display data table
- Resource $resLink data connection handle
- Array
- * */
- function Calculateparamester ($intPage, $intNums, $intLinks, $strTablename, $resLink) {
- $intPage = (int) $intPage;
- $intNums = (int) $intNums;
- $intLinks = (int) $intLinks;
- The number of links displayed is not odd when adjusted to odd
- if ($intLinks% 2 = = 0) {
- $intLinks--;
- }
- The number of records displayed per page is not greater than 0 o'clock adjusted to 10
- if ($intNums <= 0) {
- $intNums = 10;
- }
- Calculate the total number of pages
- $STRSQL 1= "SELECT COUNT (*) as num from ' {$strTablename} '";
- $RESOBJ 1=mysql_query ($strSql 1, $resLink);
- $ARROBJ 1=mysql_fetch_assoc ($resObj 1);
- $intAllRecords = $arrObj 1[' num '];
- $intAllPage =ceil ($intAllRecords/$intNums);
- The first parameter of the SQL statement limit keyword
- $intOffset = ($intPage-1) * $intNums;
- Shows the number of links not greater than 0 if only the next page of the previous page is displayed
- if ($intLinks <= 0) {
- $STRSQL 2= "SELECT * from ' {$strTablename} ' limit {$intOffset},{$intNums}";
- $arrParameter [' Start ']=null;
- $arrParameter [' End ']=null;
- $arrParameter [' page ']= $intPage;
- $arrParameter [' Nums ']= $intNums;
- $arrParameter [' Links ']=null;
- $arrParameter [' All ']= $intAllPage;
- $arrParameter [' SQL ']= $STRSQL 2;
- $arrParameter [' tag ']=1;
- Displays the number of links that are greater than 0 when the paging bar code is displayed
- }else{
- When the total record is greater than 0
- if ($intAllPage > 0) {
- Determine the value of the current page number
- if ($intPage <= 0) {
- $intPage = 1;
- }
- if ($intPage >= $intAllPage) {
- $intPage = $intAllPage;
- }
- $intHalfLinks =floor ($intLinks/2);
- Calculates the value of the start page number
- $intStartPage = $intPage-$intHalfLinks;
- if ($intStartPage <= 0) {
- $intStartPage = 1;
- }
- if (($intAllPage-$intPage) < $intHalfLinks) {
- $intStartPage = $intPage-$intHalfLinks-($intHalfLinks-($intAllPage-$intPage));
- $intStartPage = $intPage-$intHalfLinks-$intHalfLinks + $intAllPage-$intPage;
- $intStartPage = $intAllPage -2* $intHalfLinks;
- }
- Calculates the value of the end page number
- $intEndPage = $intPage + $intHalfLinks;
- if ($intEndPage < $intLinks && $intAllPage >= $intLinks) {
- $intEndPage = $intLinks;
- }
- if ($intEndPage > $intAllPage) {
- $intEndPage = $intAllPage;
- }
- Create an SQL statement to execute
- $STRSQL 2= "SELECT * from ' {$strTablename} ' limit {$intOffset},{$intNums}";
- $arrParameter [' Start ']= $intStartPage;
- $arrParameter [' End ']= $intEndPage;
- $arrParameter [' page ']= $intPage;
- $arrParameter [' Nums ']= $intNums;
- $arrParameter [' Links ']= $intLinks;
- $arrParameter [' All ']= $intAllPage;
- $arrParameter [' SQL ']= $STRSQL 2;
- $arrParameter [' tag ']=2;
- When the total record equals 0 of the case
- }else{
- $arrParameter [' Start ']=null;
- $arrParameter [' End ']=null;
- $arrParameter [' Page ']=null;
- $arrParameter [' Nums ']=null;
- $arrParameter [' Links ']=null;
- $arrParameter [' All ']=null;
- $arrParameter [' SQL ']=null;
- $arrParameter [' tag ']=3;
- }
- }
- return $arrParameter;
- }
- /**
- * Create a paging bar
- *
- * @param int $intPage The currently displayed page number value
- * @param int $intStartPage Start Page
- * @param int $intEndPage End page number
- * Total number of records @param int $intAllRecords
- * @param int $intTag page Bar type tag
- * @return String
- * */
- function Createpagingitem ($intPage, $intStartPage, $intEndPage, $intAllPage, $intTag) {
- $strPageItem = ";
- Shows the number of links not greater than 0 if only the next page of the previous page is displayed
- if ($intTag = = 1) {
- if ($intAllPage <= 0) {
- $strPageItem. = ' home Page last ';
- }else{
- if ($intPage = = 1) {
- $strPageItem. = "First Prev";
- $strPageItem. = "";
- }else{
- $strPageItem. = "Home";
- $strPageItem. = "";
- $strPageItem. = "Previous page";
- $strPageItem. = "";
- }
- if ($intPage = = $intAllPage) {
- $strPageItem. = "Next Page last";
- }else{
- $strPageItem. = "Next page";
- $strPageItem. = "";
- $strPageItem. = "Last";
- }
- }
- }
- Displays the number of links that are greater than 0 when the paging bar code is displayed
- if ($intTag = = 2) {
- if ($intPage = = 1) {
- $strPageItem. = "First Prev";
- $strPageItem. = "";
- }else{
- $strPageItem. = "Home";
- $strPageItem. = "";
- $strPageItem. = "Previous page";
- $strPageItem. = "";
- }
- for ($i = $intStartPage; $i <= $intEndPage; $i + +) {
- if ($i = = $intPage) {
- $strPageItem. = $i;
- }else{
- $strPageItem. = "[". $i. "]";
- }
- $strPageItem. = "";
- }
- if ($intPage = = $intAllPage) {
- $strPageItem. = "Next Page last";
- }else{
- $strPageItem. = "Next page";
- $strPageItem. = "";
- $strPageItem. = "Last";
- }
- }
- When the total record equals 0 of the case
- if ($intTag = = 3) {
- $strPageItem. = ' home Page last ';
- }
- return $strPageItem;
- }
- /**
- * Get and output data
- *
- * SQL statement @param string $STRSQL query
- * @param array $arrFields The fields that need to be displayed
- * @param resource $resLink data connection handle
- * @return String
- * */
- function Outputdata ($STRSQL, $arrFields, $resLink) {
- $RESOBJ =mysql_query ($STRSQL, $resLink);
- $ARROBJ =array ();
- $strOutPutData = ";
- $arrFieldsCode =array_keys ($arrFields);
- while (@ $arrRow =mysql_fetch_assoc ($RESOBJ)) {
- $ARROBJ []= $arrRow;
- }
- $strOutPutData. = "
- $strOutPutData. = '
- foreach ($arrFieldsCode as $strVal) {
- }
- foreach ($arrObj as $arrVal) {
- foreach ($arrFieldsCode as $strVal) {
- }
- }
- $strOutPutData. = "
- $strOutPutData. = "
- $strOutPutData. = "
- $strOutPutData. = "
- $strOutPutData. = "
- $strOutPutData. = "
";
';
". $arrFields [Trim ($strVal)]." | ";
";
";
". $arrVal [Trim ($strVal)]." | ";
";
";
- return $strOutPutData;
- }
- Connect and select a database
- Note: You should modify the database account and password as well as the database name
- $resLink =mysqlconnect (' localhost ', ' root ', ' root ', ' ztlibrary ');
- To find the paging parameter note: You should modify the data table name
- $arrParameter =calculateparamester (@$_get[' page ']?$_get[' page ']:1,
- 10,5, ' Book_info ', $resLink);
- The data to be displayed, the field name of the table is its key value, the Chinese of the field name is interpreted as its element value
- Note: You should modify the following array according to your data table
- $arrFields =array (' id_code ' = ' book Code ', ' book_name ' = ' book name ',
- ' BOOK_ISBN ' + ' ISBN ', ' contribute_man ' + ' source ', ' issue_time ' and ' publication Time ', ' storing_time ' + ' ' storage time ';
- ?>
- "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
- PHP Pagination Demo--www.yuju100.com
-
- Output Paging data
- Echo outputdata ($arrParameter [' SQL '], $arrFields, $resLink);
- ?>
- Show Pagination Bar
- Echo Createpagingitem ($arrParameter [' page '], $arrParameter [' Start '], $arrParameter [' End '],
- $arrParameter [' All '], $arrParameter [' tag '];
- ?>
Copy Code |