php自动适应范围的分页代码_php技巧
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 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 $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 if($i==$page){
$pagecode.=''.$i.'';
} else {
$pagecode.="$i";
}
}
if($page!=$pages){
$pagecode.=">";//下一页
$pagecode.=">>";//最后一页
}
$pagecode.="
return array('pagecode'=>$pagecode,'sqllimit'=>' limit '.$offset.','.$pagesize);
}
?>
加了页码跳转文本框
以下是新手使用说明
$phpfile = 'index.php';//页面文件名
$page= isset($_GET['page'])?$_GET['page']:1;//默认页码
$db = mysql_connect('localhost','test','test');//链接数据库
mysql_select_db('test',$db);//选择数据库
$counts = mysql_num_rows(mysql_query('select `id` from `test`',$db));//获取需要的数据总条数
$sql='select `id`,`title` from `test`';//定义查询语句SQL
$getpageinfo = page($page,$counts,$phpfile);//调用函数,生成分页HTML 和 SQL LIMIT 子句
$sql.=$getpageinfo['sqllimit'];//组合完整的SQL语句
$data = $row = array();//初始化数组
$result = mysql_query($sql,$db);//获取结果集
//将数据装入$data数组
while($row = mysql_fetch_array($result)){
$data[]=$row;
}
?>
echo $getpageinfo['pagecode'];//显示分页的html代码
?>
======================
补贴css

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
