-
- /**
- * PHP pagination code
- **/
- /**
- * Link to 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 paging and calculate the corresponding parameter value
- *Include: start page number $arrParameter['start']
- * End page number $arrParameter['end']
- * Total number of records $arrParameter['all' ]
- * Number of records displayed on each page $arrParameter['nums']
- * Number of links displayed on the page $arrParameter['links']
- * SQL statement to be queried $arrParameter['sql']
- * Type of paging bar $arrParameter['tag']
- *
- int $intPage Current page number value
- int $intNums Number of records displayed on each page
- int $intLinks Number of links displayed on the page
- string $strTablename Display data table in pages
- resource $resLink Data connection handle
- array
- **/
- function calculateParamester($intPage,$intNums,$intLinks, $strTablename,$resLink){
- $intPage=(int)$intPage;
- $intNums=(int)$intNums;
- $intLinks=(int)$intLinks;
- //When the number of displayed links is not an odd number, adjust it to an odd number.
- if($intLinks % 2 == 0){
- $intLinks--;
- }
- //Adjust to 10 when 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 sql statement limit keyword
- $intOffset=( $intPage-1)*$intNums;
- //Only display the previous page and next page, that is, the number of links displayed is not greater than 0
- if($intLinks <= 0){
- $strSql2="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']=$strSql2;
- $ arrParameter['tag']=1;
- //When the paging barcode is displayed, that is, the number of links displayed is greater than 0
- }else{
- //When the total records are 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 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;
- }
- //Calculate the value of the end page number
- $intEndPage=$intPage+$intHalfLinks;
- if($intEndPage < $intLinks && $ intAllPage > "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 ']=$strSql2;
- $arrParameter['tag']=2;
- //When the total records are equal to 0
- }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 paging bar
- *
- * @param int $intPage The currently displayed page number value
- * @param int $intStartPage The starting page number
- * @param int $intEndPage The ending page number
- * @param int $intAllRecords The total number of records
- * @param int $intTag paging bar type tag
- * @return string
- **/
- function createPagingItem($intPage,$intStartPage,$intEndPage,$intAllPage,$intTag){
- $strPageItem='';
- //Only display the previous page and the next page. The number of links displayed is not greater than 0
- if($intTag == 1){
- if($intAllPage <= 0){
- $strPageItem.='Homepage Last Page';
- }else{
- if( $intPage == 1){
- $strPageItem.="Home Previous page";
- $strPageItem.=" ";
- }else{
- $strPageItem.="Homepage";
- $strPageItem.=" ";
- $strPageItem.="Previous page";
- $strPageItem.=" ";
- }
- if($intPage == $intAllPage){
- $strPageItem.="Next page Last page";
- }else{
- $strPageItem.="Next page";
- $strPageItem.=" ";
- $strPageItem.="Last page";
- }
- }
- }
- //When displaying paging barcode, the number of links displayed is greater than 0
- if($intTag == 2){
- if($intPage == 1){
- $strPageItem.="Homepage Previous page";
- $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.="Next page Last page";
- }else{
- $strPageItem.="Next page";
- $strPageItem.=" ";
- $strPageItem.="Last page";
- }
- }
- //When the total records are equal to 0
- if($intTag == 3){
- $strPageItem.='Home page =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){
- $strOutPutData.="
".$arrFields[trim($strVal)]." | ";
- }
- $strOutPutData.="
";
- foreach($arrObj as $arrVal){
- $strOutPutData.="
";
- foreach($arrFieldsCode as $strVal){
- $strOutPutData.="
".$arrVal[trim($strVal)]." | ";
- }
- $strOutPutData.="
";
- }
- $strOutPutData.="
";
- return $strOutPutData;
- }
- // Connect and select the database
- // Note: You should modify the database account and password as well as the database name
- $resLink=mysqlConnect('localhost','root','root','ztlibrary');
- // Find out Note on paging parameters: You should modify the data table name
- $arrParameter=calculateParamester(@$_GET['page']?$_GET['page']:1,
- 10,5,'book_info',$resLink);
- / /The data to be displayed is composed of the field name of the table and its key value. The Chinese interpretation of the field name is its element value
- //Note: You should modify the following array according to your data table
- $arrFields=array('Id_code' =>'Book_name'=>'Book name',
- 'Book_ISBN'=>'ISBN','Contribute_man'=>'Source','Issue_time'=>'Publication time' ,'Storing_time'=>'Storing time');
- ?>
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- php paging demonstration--www.yuju100.com
- table{
- margin:0px;
- padding:0px;
- border-collapse:collapse;
- border:1px solid #ff0000;
- }
- table td{
- margin:0px;
- padding:0px;
- border-collapse:collapse;
- border:1px solid #ff0000;
- }
- < ;div>
- //Output paging data
- echo outPutData($arrParameter['sql'],$arrFields,$resLink);
- ?>
< ;?php
//Display paging bar
echo createPagingItem($arrParameter['page'],$arrParameter['start'],$arrParameter['end'],
$arrParameter['all'],$arrParameter[' tag']);
?>
Copy code
|