Home > Backend Development > PHP Tutorial > Quickly implement PHP paging function in two steps, convenient and practical

Quickly implement PHP paging function in two steps, convenient and practical

王林
Release: 2023-04-07 07:52:02
forward
2085 people have browsed it

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=&#39;&#39;,$filename=&#39;&#39;,$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[&#39;page&#39;];    
                if (($page!=&#39;&#39;) && (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;    
        }    
?>
Copy after login

2. Paging code for calling :

<?php    
    static private function Paging($FilesName,$PageCount,$page){    
    $PageStr="";    
    $topname=&#39;第一页&#39;;    
    $bottomname=&#39;最末页&#39;;    
    $overname=&#39;上一页&#39;;    
    $upname=&#39;下一页&#39;;    
    $p=$FilesName.&#39;page=&#39;;    
            if ($PageCount>1){    
                if ($page<=1){    
                    $page=1;    
                    $PageStr=&#39;当前第 &#39;.$page.&#39; / &#39;.$PageCount.&#39; 页 [&#39;.$topname.&#39;] [&#39;.$overname.&#39;] <a href="&#39;.$p.($page+1).&#39;">[&#39;.$upname.&#39;]</a> <a href="&#39;.$p.($PageCount).&#39;">[&#39;.$bottomname.&#39;]</a>&#39;;    
                }else if($page>=$PageCount){    
                    $page=$PageCount;    
                    $PageStr=&#39;当前第 &#39;.$page.&#39; / &#39;. $PageCount . &#39; 页 <a href="&#39;.$p.(1).&#39;">[&#39;.$topname.&#39;]</a> <a href="&#39;.$p.($page-1).&#39;">[&#39;.$overname.&#39;]</a> [&#39;.$upname.&#39;] [&#39;.$bottomname.&#39;]&#39;;    
                }else{    
                    $PageStr=&#39;当前第 &#39; . $page . &#39; / &#39;. $PageCount . &#39; 页 <a href="&#39;.$p.(1).&#39;">[&#39;.$topname.&#39;]</a> <a href="&#39;.$p.($page-1).&#39;">[&#39;.$overname.&#39;]</a> <a href="&#39;.$p.($page+1).&#39;">[&#39;.$upname.&#39;]</a> <a href="&#39;.$p.($PageCount).&#39;">[&#39;.$bottomname.&#39;]</a>&#39;;    
                }    
            }else{    
                $PageCount=1;    
                $page=1;    
                $PageStr=(&#39;当前第 &#39; . $page) . &#39; / &#39;. $PageCount . &#39; 页 [&#39;.$topname.&#39;] [&#39;.$overname.&#39;] [&#39;.$upname.&#39;] [&#39;.$bottomname.&#39;]&#39;;    
            }    
            return $PageStr;    
        }    
?>
Copy after login

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!

Related labels:
source:codefans.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template