Home >Backend Development >PHP Tutorial >PHP automatically adapts to the range of paging code

PHP automatically adapts to the range of paging code

PHPz
PHPzOriginal
2016-07-29 08:38:321001browse

<?php 
function page($page,$total,$phpfile,$pagesize=10,$pagelen=7){ 
    $pagecode = &#39;&#39;;//定义变量,存放分页生成的HTML 
    $page = intval($page);//避免非数字页码 
    $total = intval($total);//保证总记录数值类型正确 
    if(!$total) return array();//总记录数为零返回空数组 
    $pages = ceil($total/$pagesize);//计算总分页 
    //处理页码合法性 
    if($page<1) $page = 1; 
    if($page>$pages) $page = $pages; 
    //计算查询偏移量 
    $offset = $pagesize*($page-1); 
    //页码范围计算 
    $init = 1;//起始页码数 
    $max = $pages;//结束页码数 
    $pagelen = ($pagelen%2)?$pagelen:$pagelen+1;//页码个数 
    $pageoffset = ($pagelen-1)/2;//页码个数左右偏移量 
    //生成html 
    $pagecode=&#39;<div class="page">&#39;; 
    $pagecode.="<span>$page/$pages</span>";//第几页,共几页 
    //如果是第一页,则不显示第一页和上一页的连接 
    if($page!=1){ 
        $pagecode.="<a href=\"{$phpfile}?page=1\"><<</a>";//第一页 
        $pagecode.="<a href=\"{$phpfile}?page=".($page-1)."\"><</a>";//上一页 
    } 
    //分页数大于页码个数时可以偏移 
    if($pages>$pagelen){ 
        //如果当前页小于等于左偏移 
        if($page<=$pageoffset){ 
            $init=1; 
            $max = $pagelen; 
        }else{//如果当前页大于左偏移 
            //如果当前页码右偏移超出最大分页数 
            if($page+$pageoffset>=$pages+1){ 
                $init = $pages-$pagelen+1; 
            }else{ 
                //左右偏移都存在时的计算 
                $init = $page-$pageoffset; 
                $max = $page+$pageoffset; 
            } 
        } 
    } 
    //生成html 
    for($i=$init;$i<=$max;$i++){ 
        if($i==$page){ 
            $pagecode.=&#39;<span>&#39;.$i.&#39;</span>&#39;; 
        } else { 
            $pagecode.="<a href=\"{$phpfile}?page={$i}\">$i</a>"; 
        } 
    } 
    if($page!=$pages){ 
        $pagecode.="<a href=\"{$phpfile}?page=".($page+1)."\">></a>";//下一页 
        $pagecode.="<a href=\"{$phpfile}?page={$pages}\">>></a>";//最后一页 
    } 
    $pagecode.="<input type=\"text\" size=\"3\" onkeydown=\"if(event.keyCode==13) {window.location.href=&#39;{$phpfile}?page=&#39;+this.value; return false;}\" /></div>"; 
    return array(&#39;pagecode&#39;=>$pagecode,&#39;sqllimit&#39;=>&#39; limit &#39;.$offset.&#39;,&#39;.$pagesize); 
} 
?>

Added page number jump text box

The following are the instructions for beginners

<?php
$phpfile = &#39;index.php&#39;;//页面文件名
$page= isset($_GET[&#39;page&#39;])?$_GET[&#39;page&#39;]:1;//默认页码
$db = mysql_connect(&#39;localhost&#39;,&#39;test&#39;,&#39;test&#39;);//链接数据库
mysql_select_db(&#39;test&#39;,$db);//选择数据库
$counts = mysql_num_rows(mysql_query(&#39;select `id` from `test`&#39;,$db));//获取需要的数据总条数
$sql=&#39;select `id`,`title` from `test`&#39;;//定义查询语句SQL
$getpageinfo = page($page,$counts,$phpfile);//调用函数,生成分页HTML 和 SQL LIMIT 子句
$sql.=$getpageinfo[&#39;sqllimit&#39;];//组合完整的SQL语句
$data = $row = array();//初始化数组
$result = mysql_query($sql,$db);//获取结果集
//将数据装入$data数组
while($row = mysql_fetch_array($result)){
$data[]=$row;
}
?>
<?php
echo $getpageinfo[&#39;pagecode&#39;];//显示分页的html代码
?>

css:

<style type="text/css">
body{font-family:Tahoma;}
.page{padding:2px;font-weight:bolder;font-size:12px;}
.page a{border:1px solid #ccc;padding:0 5px 0 5px;margin:2px;text-decoration:none;color:#333;}
.page span{padding:0 5px 0 5px;margin:2px;background:#09f;color:#fff;border:1px solid #09c;}
</style>


Statement:
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