Home > Backend Development > PHP Tutorial > PHP paging class code example

PHP paging class code example

怪我咯
Release: 2023-03-14 11:52:01
Original
1238 people have browsed it

Pagination means dividing a page into two or more pages.

There is an automatic paging mechanism that can divide the content in the mobile Web form

into a group of smaller pages for presentation, suitable for specific equipment. This mechanism also renders user interface elements that can be used to navigate to other pages.

The following is a PHP paging code, which is relatively simple.

<?php 
function genpage(&$sql,$page_size=10) 
{ 
global $pages,$sums,$eachpage,$page; //总页数,总记录,每页数,当前页 
$page = $_GET["page"]; 
if($page ==0)$page =1; 
$eachpage = $page_size; 
$pagesql = strstr($sql," from "); 
$pagesql = "select count(*) as ids ".$pagesql; 
$conn = mysql_query($pagesql) or die(mysql_error()); 
if($rs = mysql_fetch_array($conn))$sums = $rs[0]; 
$pages=ceil($sums/$eachpage); 
if($pages==0)$pages=1; 
$startpos = ($page-1)*$eachpage; 
$sql .=" limit $startpos,$eachpage "; 
} 

//显示分页 
function showpage() 
{ 
global $pages,$sums,$eachpage,$page; //总页数,总记录,每页数,当前页,其它参数 
$link=$_SERVER[&#39;PHP_SELF&#39;]; 
echo "记录".$sums.":".$eachpage." "; 
echo "页数".$page."/".$pages." "; 
$p_head=$page-5; 
if($p_head<=0)$p_head=1; //页码循环开始数 前5个 
$p_end=$page+5; 
if($p_end>$pages)$p_end=$pages; //页码循环结束数 后5个 
echo "[<a href=$link?page=1>首页</a>] "; 
for($i=$p_head;$i<=$p_end;$i++) 
{ 
if($i!=$page) 
echo "<a href=$link?page=$i>[$i]</a> "; 
else 
echo "<b><strike>[$i]</strike></b> "; 
} 
echo " [<a href=$link?page=$pages>末页</a>]"; 
} 
?>
Copy after login

The above is the detailed content of PHP paging class code example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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