Under normal circumstances, I will make a special class for database reading, which includes operations on the database, including paging, etc., so that it is convenient to use. There are two main sections of code:
1. Read the database and convert it into a paging array:
The code is as follows:
<?php private function rs2array($sql='',$filename='',$pagesize=0){//生成二维数组 $autopage=false; if (!isset($sql))die("未设置语句!"); $str=array(); $result = $this->Open_Db($sql); $this->recordcount=$result->recordcount; if ((isset($filename)) && ($pagesize!=0)){//分页开始 $autopage=true; $FilesName = $filename; $result->pagesize=$pagesize; $page=$_GET['page']; if (($page!='') && (is_numeric($page))){ $epage = $page; if ($epage<1)$epage=1; if ($epage>$result->pagecount)$epage = $result->pagecount; }else{ $epage=1; } if(!$result->eof)$result->Absolutepage=$epage; $whileNum=$result->pagesize; } if(!isset($whileNum))$whileNum=$result->recordcount; for($i=1;$i<=$whileNum;$i++){ if($result->eof)break; for($n=0;$n<=($result->fields->count-1);$n++){ $str[$i-1][$result[$n]->name] = $result[$n]->value; } $result->movenext(); } if($autopage==true)$this->page = $this->Paging($filename,$result->pagecount,$epage); $result->close(); return $str; } ?>
2. Paging code for calling :
<?php static private function Paging($FilesName,$PageCount,$page){ $PageStr=""; $topname='第一页'; $bottomname='最末页'; $overname='上一页'; $upname='下一页'; $p=$FilesName.'page='; if ($PageCount>1){ if ($page<=1){ $page=1; $PageStr='当前第 '.$page.' / '.$PageCount.' 页 ['.$topname.'] ['.$overname.'] <a href="'.$p.($page+1).'">['.$upname.']</a> <a href="'.$p.($PageCount).'">['.$bottomname.']</a>'; }else if($page>=$PageCount){ $page=$PageCount; $PageStr='当前第 '.$page.' / '. $PageCount . ' 页 <a href="'.$p.(1).'">['.$topname.']</a> <a href="'.$p.($page-1).'">['.$overname.']</a> ['.$upname.'] ['.$bottomname.']'; }else{ $PageStr='当前第 ' . $page . ' / '. $PageCount . ' 页 <a href="'.$p.(1).'">['.$topname.']</a> <a href="'.$p.($page-1).'">['.$overname.']</a> <a href="'.$p.($page+1).'">['.$upname.']</a> <a href="'.$p.($PageCount).'">['.$bottomname.']</a>'; } }else{ $PageCount=1; $page=1; $PageStr=('当前第 ' . $page) . ' / '. $PageCount . ' 页 ['.$topname.'] ['.$overname.'] ['.$upname.'] ['.$bottomname.']'; } return $PageStr; } ?>
I think this way you basically don’t have to worry about paging problems when reading the database, and if you have multiple website columns, It can be called for paging, which is very convenient.
Please point out any deficiencies in the above code, thank you!
For more PHP-related issues, please visit the PHP Chinese website: https://www.php.cn/
The above is the detailed content of Quickly implement PHP paging function in two steps, convenient and practical. For more information, please follow other related articles on the PHP Chinese website!