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

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

王林
王林forward
2019-08-21 10:01:151912browse

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:

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 :

1){    
                if ($page<=1){    
                    $page=1;    
                    $PageStr='当前第 '.$page.' / '.$PageCount.' 页 ['.$topname.'] ['.$overname.'] ['.$upname.'] ['.$bottomname.']';    
                }else if($page>=$PageCount){    
                    $page=$PageCount;    
                    $PageStr='当前第 '.$page.' / '. $PageCount . ' 页 ['.$topname.'] ['.$overname.'] ['.$upname.'] ['.$bottomname.']';    
                }else{    
                    $PageStr='当前第 ' . $page . ' / '. $PageCount . ' 页 ['.$topname.'] ['.$overname.'] ['.$upname.'] ['.$bottomname.']';    
                }    
            }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!

Statement:
This article is reproduced at:codefans.net. If there is any infringement, please contact admin@php.cn delete