Login
网站首页 > 文章中心 > 其它

超好用的php分页类

作者:小编 更新时间:2023-09-24 10:54:45 浏览量:274人看过

php分页功能怎么实现

php本身是没有分页概念的,分页是URL传参,然后通过mysql查询语句到数据库获取数据,然后实现的分页,url上的参数,通过PHP的$_GET都是可以获取到的.

php分页类好一点的谁有

/*

* 模式四种分页模式:

require_once('../libs/classes/page.class.php');

echo 'mode:1br'.$page-show();

开启AJAX:

echo 'mode:1br'.$ajaxpage-show();

采用继承自定义分页显示模式:

*/

class page {

/**

* config ,public

var $next_page = '';//下一页

var $pre_page = '';//上一页

var $first_page = 'First';//首页

var $last_page = 'Last';//尾页

var $pre_bar = '';//上一分页条

var $next_bar = '';//下一分页条

var $format_left = '[';

var $format_right = ']';

var $is_ajax = false;//是否支持AJAX分页模式

var $is_rewrite = true;//是否启动url重写分页模式

var $totalcount = 0;

var $nowpage ;

var $perpage = 0;

* private

*

var $pagebarnum = 10;//控制记录条的个数.

var $totalpage = 0;//总页数

var $ajax_action_name = '';//AJAX动作名

var $nowindex = 1;//当前页

var $url = "";//url地址头

var $offset = 0;

* constructor构造函数

* @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']...

function page($array) {

if(is_array($array)) {

if(!array_key_exists('total',$array))

$this-error(__FUNCTION__,'need a param of total');

$this-totalcount = $total = intval($array['total']);

$perpage = (array_key_exists('perpage',$array)) ? intval($array['perpage']):10;

$this-nowpage = $nowindex = (array_key_exists('nowindex',$array)) ? intval($array['nowindex']):'';

$url = (array_key_exists('url',$array)) ? $array['url'] : '';

} else {

$total = 0;

$perpage = 10;

$nowindex = '';

$url = '';

}

if( (!is_int($total)) || ($total 0) )

$this-error(__FUNCTION__,$total.' is not a positive integer!');

if( (!is_int($perpage)) || ($perpage = 0) )

$this-error(__FUNCTION__,$perpage.' is not a positive integer!');

if(!empty($array['page_name']))

$this-set('page_name',$array['page_name']);//设置pagename

$this-_set_nowindex($nowindex);//设置当前页

$this-_set_url($url);//设置链接地址

$this-perpage = $perpage;

$cont = $total / $perpage;

$this-totalpage = floor($cont);

$this-totalpage = floor($cont)◆1;

$this-offset = ($this-nowindex-1) * $this-perpage;

if(!empty($array['ajax']))

$this-open_ajax($array['ajax']);//打开AJAX模式

* 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception

* @param string $var

* @param string $value

function set($var,$value) {

if(in_array($var,get_object_vars($this)))

$this-$var=$value;

else

$this-error(__FUNCTION__,$var." does not belong to PB_Page!");

* 打开倒AJAX模式

* @param string $action 默认ajax触发的动作.

function open_ajax($action) {

$this-is_ajax = true;

$this-ajax_action_name = $action;

* 获取显示"下一页"的代码

* @param string $style

* @return string

function next_page($style='') {

if($this-nowindex$this-totalpage) {

return $this-_get_link($this-_get_url($this-nowindex◆1),$this-next_page,$style);

return 'span class="'.$style.'"'.$this-next_page.'/span';

* 获取显示"上一页"的代码

function pre_page($style='') {

if($this-nowindex 1) {

return $this-_get_link($this-_get_url($this-nowindex-1),$this-pre_page,$style);

return 'span class="'.$style.'"'.$this-pre_page.'/span';

* 获取显示"首页"的代码

function first_page($style='') {

if($this-nowindex==1) {

return 'span class="'.$style.'"'.$this-first_page.'/span';

return $this-_get_link($this-_get_url(1),$this-first_page,$style);

* 获取显示"尾页"的代码

function last_page($style=''){

if( $this-nowindex == $this-totalpage ) {

return 'span class="'.$style.'"'.$this-last_page.'/span';

return $this-_get_link($this-_get_url($this-totalpage),$this-last_page,$style);

function nowbar($style='',$nowindex_style='') {

if($this-pagebarnum - $plus ◆ $this-nowindex $this-totalpage ) {

$plus = ($this-pagebarnum - $this-totalpage ◆ $this-nowindex);

$begin = $this-nowindex - $plus ◆ 1;

$begin = ( $begin = 1) ? $begin : 1;

$return = '';

for($i = $begin; $i $begin ◆ $this-pagebarnum ; $i◆◆) {

if( $i = $this-totalpage ) {

if( $i != $this-nowindex ) {

$return .= $this-_get_text($this-_get_link($this-_get_url($i),$i,$style));

$return .= $this-_get_text('span class="'.$nowindex_style.'"'.$i.'/span');

break;

$return .= "\n";

unset($begin);

return $return;

* 获取显示跳转按钮的代码

function select() {

$return = 'select id="PB_Page_Select" onChange="window.location.href = \''.$this-url.'\'◆this.value;" ';

for( $i=1 ; $i = $this-totalpage ; $i◆◆ ){

if( $i == $this-nowindex ) {

$return .= 'option value="'.$i.'" selected'.$i.'/option';

$return .= 'option value="'.$i.'"'.$i.'/option';

unset($i);

$return .= '/select';

* 获取mysql 语句中limit需要的值

function offset() {

return $this-offset;

* 控制分页显示风格(你可以增加相应的风格)

* @param int $mode

function show($mode=1) {

$str = "共 {$this-totalcount} 条记录 ";

if($this-nowpage 0)

$str .= " 第{$this-nowpage}/{$this-totalpage}页 ";

switch ($mode){

case '1':

$this-next_page = '下一页';

$this-pre_page = '上一页';

return $str . $this-pre_page().'?'.$this-nowbar().'?'.$this-next_page().'?'.'第'.$this-select().'页';

$this-next_page = '下一页';

$this-pre_page = '上一页';

$this-first_page = '首页';

$this-last_page = '尾页';

return $str . $this-first_page().$this-pre_page().'[第'.$this-nowindex.'页]'.$this-next_page().$this-last_page().'第'.$this-select().'页';

return $str . $this-first_page().'?'.$this-pre_page().'?'.$this-next_page().'?'.$this-last_page();

return $str . $this-pre_page().$this-nowbar().$this-next_page();

return $str . $this-pre_bar.$this-pre_page().$this-nowbar().$this-next_page().$this-next_bar;

/*----------------private function (私有方法)-----------------------------------------------------------*/

* 设置url头地址

* @param: String $url

* @return boolean

function _set_url($url="") {

if(!empty($url)) {

//手动设置

$this-url = $url.((stristr($url,'?'))?'':'?').$this-page_name."=";

//自动获取

if(empty($_SERVER['QUERY_STRING'])) {

//不存在QUERY_STRING时

#如果开启了url重写

if($this-is_rewrite) {

$this-url = $str .'p/';

$this-url = $_SERVER['REQUEST_URI']."?".$this-page_name."=";

if(stristr($_SERVER['QUERY_STRING'],$this-page_name.'=')) {

//地址存在页面参数

$this-url = str_replace($this-page_name.'='.$this-nowindex,'',$_SERVER['REQUEST_URI']);

$last = $this-url[strlen($this-url)-1];

if($last=='?'||$last=='') {

$this-url .= $this-page_name."=";

$this-url .= ''.$this-page_name."=";

$this-url = $_SERVER['REQUEST_URI'].''.$this-page_name.'=';

}//end if

* 设置当前页面

function _set_nowindex($nowindex) {

if(empty($nowindex)) {

//系统获取

if(isset($_GET[$this-page_name])) {

$this-nowindex = intval($_GET[$this-page_name]);

$this-nowindex = intval($nowindex);

* 为指定的页面返回地址值

* @param int $pageno

* @return string $url

function _get_url($pageno=1) {

return $this-url.$pageno;

* 获取分页显示文字,比如说默认情况下_get_text('a href=""1/a')将返回[a href=""1/a]

* @param String $str

function _get_text($str) {

return $this-format_left.$str.$this-format_right;

* 获取链接地址

function _get_link($url,$text,$style='') {

$style = (empty($style))?'':'class="'.$style.'"';

if($this-is_ajax) {

//如果是使用AJAX模式

return 'a '.$style.' href="javascript:'.$this-ajax_action_name.'(\''.$url.'\')"'.$text.'/a';

return 'a '.$style.' href="'.$url.'"'.$text.'/a';

* 出错处理方式

function error($function,$errormsg) {

die('Error in file b'.__FILE__.'/b ,Function b'.$function.'()/b :'.$errormsg);

找个简单实用的php分页类30分

html xmlns=""

head

meta http-equiv="Content-Type" content="text/html; charset=gbk" /

title通讯录/title

script type="text/javascript"

function onsure(url){

var sure = confirm("确定要删除?");

if(sure){

location.href = url;

}else{

return false;

/script

style

a:hover{background-color:#ccc;}

/style

/head

body

php if(!empty($_SESSION['user'])){ ?

marquee土嘎嘎的粉丝们大家好,欢迎"?php echo $_SESSION['user'] //$_COOKIE['user'] ?"回来!a href="unset.php"安全退出/a/marquee

php } ?

table width='100%' border=1

caption align="top"h1通讯录/h1/caption

tr

form action="index.php" method="get"

input type="text" name="key" value="?php echo $_GET['key'] ?" /

input type="submit" value="查询" /

/form

/th

/tr

th用户名/th

th性别/th

th爱好/th

th地址/th

th手机/th

th座机/th

th操作/th

php } ?

php

include "connect.php";

/********查询***********/

#获取查询关键字

$key = $_GET['key'];

#求记录总数

$sql = "select count(*) from user where user like '%$key%'";

$query = mysql_query($sql);

$row = mysql_fetch_row($query);

$countnum = $row[0];

#每页显示的记录数

#总页数

$countPage = ceil($countnum/$numrows);

#当前页

$page = intval($_GET['page']) 0 ? intval($_GET['page']) : 1;

#控制分栏页码

#求开始页码

$start = 1;

$start = $page - $half;

#求结束页码

$end = $countPage;

$end = $start ◆ $sort - 1;

#求出偏移量

$offset = ($page - 1) * $numrows;

$sql = "select * from user where user like '%$key%' limit $offset,$numrows";

while($val = mysql_fetch_assoc($query)){

td?php echo str_replace($key,"font color='red'$key/font",$val['user']) ?/td

td?php echo $val['sex'] == 1 ? '先生' : '女士'; ?/td

td?php echo $val['like'] ?/td

td?php echo $val['address'] ?/td

td?php echo $val['mobeil'] ?/td

td?php echo $val['tel'] ?/td

td align="center"a href="#" onclick="onsure('del.php?id=?php echo $val['id'] ?')"删除/a?|?a href="show.php?id=?php echo $val['id'] ?"修改/a/td

总共?php echo $countPage; ?页/当前第?php echo $page ?页?

a href="index.php?page=1key=?php echo $key?"首页/a?

a href="?page=?php echo $page-1 ?key=?php echo $key?"上一页/a?

php for($i = $start; $i= $end; $i◆◆){

if($i == $page){

echo $i . '? ';

a href="?page=?php echo $i ?key=?php echo $key?"?php echo $i ?/a?

} ?

!--a href="?page=?php echo $page◆1 ?".../a?

a href="?page=?php echo $countPage; ?"?php echo $countPage; ?/a?--

a href="?page=?php echo $page◆1 ?key=?php echo $key?"下一页/a?

a href="?page=?php echo $countPage; ?key=?php echo $key?"尾页/a?

/table

a href="form.html"添加新联系人/a

a href="login.html"管理员登陆/a

/body

/html

求PHP各种分页方法,各位大仙有的别藏了~~

PHP分页方法:

第一步创建一个类 subPages.php?php

class SubPages{

private $each_disNums;//每页显示的条目数

private $nums;//总条目数

private $current_page;//当前被选中的页

private $sub_pages;//每次显示的页数

private $pageNums;//总页数

private $page_array = array();//用来构造分页的数组

private $subPage_link;//每个分页的链接

private $subPage_type;//显示分页的类型

function __construct($each_disNums,$nums,$current_page,$sub_pages,$subPage_link,$subPage_type){

//intval通过使用特定的进制转换(默认是十进制),返回变量 var 的 integer 数值

$this-each_disNums=intval($each_disNums);

$this-nums=intval($nums);

if(!$current_page){

$this-current_page=1;

$this-current_page=intval($current_page);

$this-sub_pages=intval($sub_pages);

$this-pageNums=ceil($nums/$each_disNums);

$this-subPage_link=$subPage_link;

$this-show_SubPages($subPage_type);

//echo $this-pageNums."--".$this-sub_pages;

//__destruct析构函数,当类不在使用的时候调用,该函数用来释放资源

function __destruct(){

unset($each_disNums);

unset($nums);

unset($current_page);

unset($sub_pages);

unset($pageNums);

unset($page_array);

unset($subPage_link);

unset($subPage_type);

//show_SubPages函数用在构造函数里面.而且用来判断显示什么样子的分页

function show_SubPages($subPage_type){

if($subPage_type == 1){

$this-subPageCss1();

//用来给建立分页的数组初始化的函数.

function initArray(){

for($i=0;$i$this-sub_pages;$i◆◆){

$this-page_array[$i]=$i;

return $this-page_array;

//construct_num_Page该函数使用来构造显示的条目

function construct_num_Page(){

if($this-pageNums $this-sub_pages){

$current_array=array();

for($i=0;$i$this-pageNums;$i◆◆){

$current_array[$i]=$i◆1;

$current_array=$this-initArray();

for($i=0;$icount($current_array);$i◆◆){

}elseif ($this-current_page = $this-pageNums $this-current_page $this-pageNums - $this-sub_pages ◆1 ){

$current_array[$i]=($this-pageNums)-($this-sub_pages)◆1◆$i;

return $current_array;

//构造普通模式的分页

function subPageCss1(){

$subPageCss1Str="";

$subPageCss1Str.="共".$this-nums."条记录,";

$subPageCss1Str.="每页显示".$this-each_disNums."条,";

$subPageCss1Str.="当前第".$this-current_page."/".$this-pageNums."页 ";

if($this-current_page 1){

$firstPageUrl=$this-subPage_link."1";

$prewPageUrl=$this-subPage_link.($this-current_page-1);

$subPageCss1Str.="[a href='$firstPageUrl'首页/a] ";

$subPageCss1Str.="[a href='$prewPageUrl'上一页/a] ";

}else {

$subPageCss1Str.="[首页] ";

$subPageCss1Str.="[上一页] ";

if($this-current_page $this-pageNums){

$lastPageUrl=$this-subPage_link.$this-pageNums;

$nextPageUrl=$this-subPage_link.($this-current_page◆1);

$subPageCss1Str.=" [a href='$nextPageUrl'下一页/a] ";

$subPageCss1Str.="[a href='$lastPageUrl'尾页/a] ";

$subPageCss1Str.="[下一页] ";

$subPageCss1Str.="[尾页] ";

echo $subPageCss1Str;

//构造经典模式的分页

$a=$this-construct_num_Page();

for($i=0;$icount($a);$i◆◆){

$s=$a[$i];

if($s == $this-current_page ){

$url=$this-subPage_link.$s;

要做分页的页面?php//每页显示的条数

//得到当前是第几页

$pageCurrent=$_GET["p"];

$offet =($pageCurrent-1)*$page_size;

//建立数据库连接

$conn = mysql_connect("地址","用户名","密码") or die("".mysql_error());

//设置字符集

mysql_query("set names gbk");

//设置数据库

mysql_select_db("数据库名",$conn) or die("".mysql_error());

if($conn) { include ("query.html");

//查询会员信息//注:这里的sql查询了两遍.第一遍是确定查询出来的总数据条数

$query_sql = "SELECT * FROM user_info_t order by id desc";//第二遍是根据需要即$page_size来确定每次查出的条数

$query_sql_sec= "SELECT * FROM user_info_t order by id desc limit $offet,$page_size";

//执行查询语句 返回一个结果集

$query_res = mysql_query($query_sql,$conn);

$query_res_sec = mysql_query($query_sql_sec,$conn);

//查询列数

//$query_cols = mysql_num_fields($query_res);

//查询行数

$query_num =mysql_num_rows($query_res);

//返回根据从结果集取得的行生成的数组,如果没有更多行

while($query_row = mysql_fetch_array($query_res_sec)) {

echo "tr";

for ($i=0;$i$query_cols;$i◆◆) {

//echo "input type='button' class='button' id='modify_record' name='modify_record' value='修改' /";

echo"button onClick=\"submit(location.href='../modify/modify.php?rec_id=$query_row[0]');\"class=\"button\";修改/button";

//echo "ahref='../delete/delete.php?rec_id=$query_row[0]'onClick=\"if(confirm('确实要删除此条记录吗?')) return true;else returnfalse;\"img src=\"../image/delete.gif\" align=\"absmiddle\"/删除/a";

echo "buttononClick=\"if(confirm('确实要删除此条记录吗?')) return submit(location.href='../delete/delete.php?rec_id=$query_row[0]');else return false;\"class=\"button\";删除/button";

echo "/td";

echo "/tr";

//echo "/form";

echo "/table";

require_once("../common/subPages.php");//总条目数为数据库中查询出的数据条数

$nums=$query_num;

//每次显示的页数

$sub_pages=10; //if(!$pageCurrent) $pageCurrent=1;

//调用类SubPages 传的参数为:每页显示条数/当前第几页/每次显示的页数/分页模式

$subPage=new SubPages($page_size,$nums,$pageCurrent,$sub_pages,"query.php?p=",1);

die('数据库连接异常: '.mysql_error());

//断开数据库连接

if ($conn)

{

mysql_close($conn);

以上就是土嘎嘎小编为大家整理的超好用的php分页类相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章