经典php分页代码,支持数字偏移
这款php分页代码是一款完整的php mysql css的经典分页功能,很适合于大分类有像phpwindow dz等论坛效果.
CSS样式代码如下:
- <style type="text/css">
- <!--
- body {
- width:800px;
- margin:0 auto;
- margin-top:50px;
- font-size:12px;
- }
- a {
- color:#014ccc;
- text-decoration:none;
- }
- .div {
- float:left;
- cursor:pointer;
- font-weight:bold;
- margin-right:5px;
- display: block;
- padding:3px 7px;
- text-align:center;
- border:#bbdded solid 1px;
- }
- .divs {
- float:left;
- font-weight:bold;
- margin-right:5px;
- display: block;
- padding:3px 7px;
- text-align:center;
- border:#cccccc solid 1px;
- }
- .divsi {
- float:left;
- font-weight:bold;
- margin-right:5px;
- display: block;
- padding:3px 7px;
- text-align:center;
- background:#3399ff;
- color:#ffffff;
- border:#cccccc solid 1px;
- }
- .div:hover {
- background:#3399ff;
- color:#ffffff;
- }
- .divsi:hover {
- background:#3399ff;
- color:#ffffff;
- }
- -->
- </style>
PHP实例效果代码如下:
- <?php
- mysql_connect('localhost','root','');
- mysql_select_db('news');
- mysql_query('set names gbk');
-
- $sql = mysql_query("select * from `v_char`");
- $num = mysql_num_rows($sql);
- include 'page.class.php';
-
-
-
-
-
-
-
-
- $model = new getpages(10,$num,$_get['page']);
-
- $sql = "select * from `v_char` limit $model->offset,$model->pagesize";
- $res = mysql_query($sql);
- while($row = mysql_fetch_assoc($res))
- {
- echo $row['id'] . " - " . $row['name'] .'<br>';
- }
-
-
- echo $model->getpage();
- ?>
-
- page.class.php
-
- <?php
-
-
-
-
- class getpages
- {
- public $pagesize;
- public $offset;
- public $pagenum;
- public $page;
- public $init;
- public $page_len;
- public $pages;
-
-
- function __construct($pagesize,$pagenum,$page,$init=1,$page_len=7)
- {
- $this->pagesize = $pagesize;
- $this->pagenum = $pagenum;
- $this->page = $page;
- $this->pages = ceil($this->pagenum/$this->pagesize);
- if(emptyempty($this->page) || $this->page <1 || $this->page > $this->pages)$this->page=1;
- $this->init = $init;
- $this->page_len = $page_len;
- $this->offset = ($this->page-1)*$this->pagesize;
- }
-
-
- function getpage()
- {
- $this->prepg = $this->page-1;
- $this->nextpg= ($this->page == $this->pages) ? 0 : $this->page+1;
-
- $pagenav = "<span class='divs'>共 ".$this->pages."页".$this->pagenum."条</span>";
- if($this->page == 1)
- {
- $pagenav .="<a href='#' class='div'>首页</a> ";
- $pagenav .="<a href='#' class='div'>上一页</a> ";
- }
- else
- {
- $pagenav .="<a href='".$this->get_url()."=1' class='div'>首页</a> ";
- $pagenav .="<a href='".$this->get_url()."=".$this->prepg."' class='div'>上一页</a> ";
- }
-
- $pagenav .= $this->getvalpage();
-
- if($this->page == $this->pages)
- {
- $pagenav .="<a href='#' class='div'>下一页</a> ";
- $pagenav .="<a href='#' class='div'>尾页</a> ";
- }
- else
- {
- $pagenav .="<a href='".$this->get_url()."=".$this->nextpg."' class='div'>下一页</a> ";
- $pagenav .="<a href='".$this->get_url()."=".$this->pages."' class='div'>尾页</a> ";
- }
-
-
-
-
-
- 代码如下 复制代码
- $pagenav .= "<select name='page' class='select' onchange='window.location.href="".$this->get_url()."="+this.value'> ";
-
- for($i=1;$i<=$this->pages;$i++)
- {
- if($i == $this->page){
- $pagenav .= "<option value='".$i."' selected>".$i."</option> ";
- }
- else
- {
- $pagenav .= "<option value='".$i."'>".$i."</option> ";
- }
- }
- $pagenav.="</select> ";
-
- return $pagenav;
- }
-
-
- function getvalpage()
- {
- $max_p = $this->pages;
- $this->page_len = ($this->page_len%2) ? $this->page_len : $this->page_len+1;
- $pageoffset = ($this->page_len-1)/2;
-
-
- if($this->pages > $this->page_len)
- {
-
- if($pageoffset>=$this->page)
- {
- $this->init = 1;
- $max_p= $this->page_len;
- }
- else
- {
-
- if($this->page+$pageoffset > $this->pages)
- {
- $this->init = $this->pages-$this->page_len+1;
- }
- else
- {
-
- $this->init = $this->page-$pageoffset;
- $max_p = $this->page+$pageoffset;
- }
- }
- }
-
-
- for($i=$this->init ;$i<=$max_p;$i++)
- {
-
- if($i == $this->page)
- {
- $pagenav .= "<a href='#' class='divsi'>".$i."</a>";
- }
- else
- {
- $pagenav .= "<a href='".$this->get_url()."=".$i."' class='div'>".$i."</a>";
- }
- }
- return $pagenav;
- }
-
-
- function get_url()
- {
- $url = $_server['request_uri'];
- $parse_url = parse_url($url);
- $query_url = $parse_url['query'];
-
- if($query_url)
- {
-
- $query_url = ereg_replace("(^|&)page=$this->page","",$query_url);
- $url = str_replace($parse_url['query'],$query_url,$url);
- if($query_url)$url .= '&page';
- else $url .= 'page';
- }
- else
- {
- $url .= '?page';
- }
- return $url;
- }
- }
-
- ?>
数据库代码如下:
- /*
- navicat mysql data transfer
-
- source server : localhost_3306
- source server version : 50051
- source host : localhost:3306
- source database : news
-
- target server type : mysql
- target server version : 50051
- file encoding : 65001
-
- date: 2010-03-30 00:17:08
- */
-
- set foreign_key_checks=0;
-
-
-
- drop table if exists `v_char`;
- create table `v_char` (
- `id` int(10) not null auto_increment,
- `name` char(10) default null,
- primary key (`id`)
- ) engine=innodb auto_increment=401 default charset=gbk;
-
-
-
-
- insert into `v_char` values ('1', 'test1');
- insert into `v_char` values ('2', 'test2');
- insert into `v_char` values ('3', 'test3');
- insert into `v_char` values ('4', 'test4');
- insert into `v_char` values ('5', 'test5');
- insert into `v_char` values ('6', 'test6');
- insert into `v_char` values ('7', 'test7');
- insert into `v_char` values ('8', 'test8');
- insert into `v_char` values ('9', 'test9');
- insert into `v_char` values ('10', 'test10');
- insert into `v_char` values ('11', 'test11');
- insert into `v_char` values ('12', 'test12');