- /**
- * PHP paging code
- **/
- /**
- * Link to the database
- * Bbs.it-home.org
- * @ Param string $ strHost address of the database server host
- * @ 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 database encoding
- Mysql_query ('set names utf8', $ resLink );
- $ IsValidate = mysql_select_db ($ strDBname, $ resLink );
- If ($ isValidate)
- {
- Return $ resLink;
- }
- Else
- {
- Return false;
- }
- }
- }
- /**
- * Accept the current page number and calculate the corresponding parameter value
- * Including: 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 ['link']
- * SQL statement $ arrParameter ['SQL']
- * Page entry type $ arrParameter ['tag']
- *
- Int $ intPage current page number
- Int $ intNums number of records per page
- Number of links displayed on the int $ intLinks page
- String $ strTablename display data tables by page
- Resource $ resLink data connection handle
- Array
- **/
- Function calculateParamester ($ intPage, $ intNums, $ intLinks, $ strTablename, $ resLink ){
- $ IntPage = (int) $ intPage;
- $ IntNums = (int) $ intNums;
- $ IntLinks = (int) $ intLinks;
- // Adjust to an odd number when the number of displayed links is not an odd number
- If ($ intLinks % 2 = 0 ){
- $ IntLinks --;
- }
- // Adjust to 10 if the number of records displayed on each page is not greater than 0
- If ($ intNums <= 0 ){
- $ IntNums = 10;
- }
- // Calculate the total number of pages
- $ StrSql1 = "select count (*) as num from '{$ strTablename }"';
- $ ResObj1 = mysql_query ($ strSql1, $ resLink );
- $ ArrObj1 = mysql_fetch_assoc ($ resObj1 );
- $ IntAllRecords = $ arrObj1 ['num'];
- $ IntAllPage = ceil ($ intAllRecords/$ intNums );
- // The first parameter of the limit keyword in the SQL statement
- $ Effecffset = ($ intPage-1) * $ intNums;
- // Only displays the next page of the previous page. that is, the number of links displayed is not greater than 0.
- If ($ intLinks <= 0 ){
- $ StrSql2 = "select * from '{$ strTablename}' limit {$ effecffset}, {$ intNums }";
- $ ArrParameter ['start'] = null;
- $ ArrParameter ['end'] = null;
- $ ArrParameter ['Page'] = $ intPage;
- $ ArrParameter ['nums'] = $ intNums;
- $ ArrParameter ['link'] = null;
- $ ArrParameter ['all'] = $ intAllPage;
- $ ArrParameter ['SQL'] = $ strSql2;
- $ ArrParameter ['tag'] = 1;
- // When a paging bar code is displayed, the number of links displayed is greater than 0.
- } Else {
- // When the total number of records 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 );
- // Calculate the start page number
- $ IntStartPage = $ intPage-$ intHalfLinks;
- If ($ intStartPage <= 0 ){
- $ IntStartPage = 1;
- }
- If ($ intAllPage-$ intPage) <$ intHalfLinks ){
- // $ IntStartPage = $ intPage-$ intHalfLinks-($ intAllPage-$ intPage ));
- // $ IntStartPage = $ intPage-$ intHalfLinks + $ intAllPage-$ intPage;
- $ IntStartPage = $ intAllPage-2 * $ intHalfLinks;
- }
- // Calculate the end page number
- $ IntEndPage = $ intPage + $ intHalfLinks;
- If ($ intEndPage <$ intLinks & $ intAllPage >=$ intLinks ){
- $ IntEndPage = $ intLinks;
- }
- If ($ intEndPage> $ intAllPage ){
- $ IntEndPage = $ intAllPage;
- }
- // Create the SQL statement to be executed
- $ StrSql2 = "select * from '{$ strTablename}' limit {$ effecffset}, {$ intNums }";
- $ ArrParameter ['start'] = $ intStartPage;
- $ ArrParameter ['end'] = $ intEndPage;
- $ ArrParameter ['Page'] = $ intPage;
- $ ArrParameter ['nums'] = $ intNums;
- $ ArrParameter ['link'] = $ intLinks;
- $ ArrParameter ['all'] = $ intAllPage;
- $ ArrParameter ['SQL'] = $ strSql2;
- $ ArrParameter ['tag'] = 2;
- // When the total number of records is equal to 0
- } Else {
- $ ArrParameter ['start'] = null;
- $ ArrParameter ['end'] = null;
- $ ArrParameter ['Page'] = null;
- $ ArrParameter ['nums'] = null;
- $ ArrParameter ['link'] = null;
- $ ArrParameter ['all'] = null;
- $ ArrParameter ['SQL'] = null;
- $ ArrParameter ['tag'] = 3;
- }
- }
- Return $ arrParameter;
- }
- /**
- * Create a page entry
- *
- * @ Param int $ the current page number displayed on intPage
- * @ Param int $ intStartPage start page number
- * @ Param int $ intEndPage ending page number
- * @ Param int $ total number of intAllRecords records
- * @ Param int $ intTag the type of the paging bar
- * @ Return string
- **/
- Function createPagingItem ($ intPage, $ intStartPage, $ intEndPage, $ intAllPage, $ intTag ){
- $ StrPageItem = '';
- // Only displays the next page of the previous page. that is, the number of links displayed is not greater than 0.
- If ($ intTag = 1 ){
- If ($ intAllPage <= 0 ){
- $ StrPageItem. = 'homepage end page ';
- } Else {
- If ($ intPage = 1 ){
- $ StrPageItem. = "homepage previous ";
- $ StrPageItem. = "";
- } Else {
- $ StrPageItem. = "homepage ";
- $ StrPageItem. = "";
- $ StrPageItem. = "previous page ";
- $ StrPageItem. = "";
- }
- If ($ intPage = $ intAllPage ){
- $ StrPageItem. = "Last page of the next page ";
- } Else {
- $ StrPageItem. = "next page ";
- $ StrPageItem. = "";
- $ StrPageItem. = "Last page ";
- }
- }
- }
- // When a paging bar code is displayed, the number of links displayed is greater than 0.
- If ($ intTag = 2 ){
- If ($ intPage = 1 ){
- $ StrPageItem. = "homepage previous ";
- $ StrPageItem. = "";
- } Else {
- $ StrPageItem. = "homepage ";
- $ StrPageItem. = "";
- $ StrPageItem. = "previous page ";
- $ StrPageItem. = "";
- }
- For ($ I = $ intStartPage; $ I <= $ intEndPage; $ I ++ ){
- If ($ I = $ intPage ){
- $ StrPageItem. = $ I;
- } Else {
- $ StrPageItem. = "[". $ I. "]";
- }
- $ StrPageItem. = "";
- }
- If ($ intPage = $ intAllPage ){
- $ StrPageItem. = "Last page of the next page ";
- } Else {
- $ StrPageItem. = "next page ";
- $ StrPageItem. = "";
- $ StrPageItem. = "Last page ";
- }
- }
- // When the total number of records is equal to 0
- If ($ intTag = 3 ){
- $ StrPageItem. = 'homepage end page ';
- }
- Return $ strPageItem;
- }
- /**
- * Obtain and output data
- *
- * @ Param string $ SQL statement queried by strSql
- * @ Param array $ arrFields an array composed of fields 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 to and select a database
- // Note: you should modify the database account, password, and database name.
- $ ResLink = mysqlConnect ('localhost', 'root', 'root', 'ztlibrary ');
- // Obtain the paging parameters. 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 Table field name is its key value, and the field name is interpreted as its element value in Chinese.
- // Note: you should modify the following array based on your data table.
- $ ArrFields = array ('id _ code' => 'Book encoding', 'Book _ name' => 'Book name ',
- & Apos; Book _ ISBN & apos; = & apos; ISBN & apos;, & apos; composite _ man & apos; = & apos; source & apos;, & apos; Issue _ time & apos ', 'storing _ time' => 'Warehouse receiving time ');
- ?>
- Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
- Php paging demonstration --www.yuju100.com
-
- // Output paging data
- Echo outPutData ($ arrParameter ['SQL'], $ arrFields, $ resLink );
- ?>
- // Display pagination bar
- Echo createPagingItem ($ arrParameter ['Page'], $ arrParameter ['start'], $ arrParameter ['end'],
- $ ArrParameter ['all'], $ arrParameter ['tag']);
- ?>
|