Home  >  Article  >  Backend Development  >  PHP automatically adapts to the range of paging code_PHP tutorial

PHP automatically adapts to the range of paging code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:50:26797browse

Copy code The code is as follows:

function page($page,$total,$phpfile,$pagesize=10,$pagelen=7){
$pagecode = '';//定义变量,存放分页生成的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='
';
    $pagecode.="$page/$pages";//第几页,共几页
    //如果是第一页,则不显示第一页和上一页的连接
    if($page!=1){
        $pagecode.="<<";//第一页
        $pagecode.="<";//上一页
    }
    //分页数大于页码个数时可以偏移
    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.=''.$i.'';
        } else {
            $pagecode.="$i";
        }
    }
    if($page!=$pages){
        $pagecode.=">";//下一页
        $pagecode.=">>";//最后一页
    }
    $pagecode.="
";
return array('pagecode'=>$pagecode,'sqllimit'=>' limit '.$offset.','.$pagesize);
}
?>

Added page number jump text box
The following are the instructions for beginners
Copy the code The code is as follows:

$phpfile = 'index.php';//Page file name
$page= isset($_GET['page'])?$_GET['page'] :1;//Default page number
$db = mysql_connect('localhost','test','test');//Link database
mysql_select_db('test',$db);//Select database
$counts = mysql_num_rows(mysql_query('select `id` from `test`',$db));//Get the total number of data required
$sql='select `id`,`title` from `test`';//Define the query statement SQL
$getpageinfo = page($page,$counts,$phpfile);//Call the function to generate paging HTML and SQL LIMIT clause
$sql.=$ getpageinfo['sqllimit'];//Combine the complete SQL statement
$data = $row = array();//Initialize the array
$result = mysql_query($sql,$db);//Get the result Set
//Load the data into the $data array
while($row = mysql_fetch_array($result)){
$data[]=$row;
}
?>
echo $getpageinfo['pagecode'];//Display the paging html code
?>

========== ============
Subsidy css
Copy code The code is as follows:

< ;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;}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319325.htmlTechArticleCopy the code as follows: ?php functionpage($page,$total,$phpfile,$pagesize=10,$ pagelen=7){ $pagecode='';//Define variables to store the HTML generated by paging $page=intval($page);//Avoid non-numbers...
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