1.html页面引入jq和js插件
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="./toushu/js/jquery.pagination2.js?_t=0.1"></script>
css样式,可以自己修改
<style type="text/css">
/*分页样式*/
.pager .nextpage{
background-position: 45px 7px;
margin: 0;
}
.pager .prepage_g{
background-position: 2px 8px;
padding:2px 8px;
}
.pager{
float:left;
margin-left:19%;
}
.pager ul li{
float:left;
}
.pager_a{
float:right;
}
.pager .total {
display:block;
float: left;
padding: 2px 8px;
text-align: center;
color: #000;
background: #fff;
border: 1px solid #ccc;
}
.pager .nextpage,.pager .nextpageg_g,.pager .prepage_g,.pager .prepage{
padding: 2px 8px;;
}
.pager .nextpage a,.prepage a{
font-size:14px;
}
.pager .prepage,{background-position: 2px 7px;}
.pager .no1,.pager .no2{padding:2px 8px;}
.pager .no1 a,.pager .no2 a{
font-size:14px;
}
/*分页样式end*/
</style>
2.显示div和js, jump函数可以通过ajax传递当前页面给后台,实现ajax分页。
<div class='page_line page_line1' style="text-align: center; margin-top: 20px;" ></div>
<script type="text/javascript">
$('.page_line1').pagination({
totalData: <?=$count?>,
showData : <?=$pagesize?>,
jump:true,
coping:true,
count:3,
current: <?=$_GET['page'] ?>,
homePage:'首页',
endPage:'末页',
prevContent:'上一页',
nextContent:'下一页',
callback:function(api){
jump(api.getCurrent());
}
});
function jump(page){
//点击页码时的跳转
window.location.href= '<?=$url?page=?>'+page;
}
</script>
后台代码:
// 分页
include "./toushu/page.php";
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if($pager->intBeginDate < 0) $pager->intBeginDate = 0;
$pagesize = 20;
$count = $_weixin_yanfang_user_prize->_listcount($sql_add);
//传入两个参数即可
$pager = new Pager($count,$pagesize);
$page_info = $pager->getPageInfo();
$list = $_weixin_yanfang_user_prize->_list($pager->intBeginDate, $pagesize, "*" ,$sql_add);
page类,可直接用
<?php
/*
*
* 特点:
*
* 一般情况下只要给这个类2个参数就能正常运行,使用很方便。
* URL中有其他变量时,也会自动加入相应的变量。
*
* 方法:
*
* Pager Pager(int intTotalDate, int intNumOfPage, [int intNumOfBar],[stringstrPageVar]);
* string show($a = '页次:', $b = '/', $c = ' 每页:', $d = ' 共计:')
* string parse()
*
* 属性:
*
* intTotalDate 总记录数
* intNumOfPage 每页显示记录数
* strPageVar 翻页变量名,默认为“PAGEID”
* intPageTotal 总页数
* intBeginDate 本页开始的记录序号
* intPage 当前页数
* intNumOfDate 当前页面显示的记录数
* intUpPage 上一页的页数
* intPageBegin 翻页条最开始的页数
*
* 用法
$pager = new Pager(912,25,5,"b");
echo $page_info = $pager->getPageInfo();
*
*/
class Pager
{
protected $first_page = '首页 ';
protected $pre_page = '上一页';
protected $next_page = '下一页';
protected $last_page = ' 尾页';
protected $sign = '/';
private $tag = '';
/*
* 构造函数
*/
public function __construct($intTotalDate, $intNumOfPage, $intNumOfBar = 9, $strPageVar = 'page') {
$this->intTotalDate = $intTotalDate;
$this->intNumOfPage = $intNumOfPage;
$this->strPageVar = $strPageVar;
$this->intPageTotal = ceil($intTotalDate/$intNumOfPage);
//当前页数没有则为1
if (!isset($_GET[$this->strPageVar]) or $_GET[$this->strPageVar] < 1) {
$_GET[$this->strPageVar] = 1;
}
//当前页数不允许超出最大页数
if ($_GET[$this->strPageVar] > $this->intPageTotal) {
$_GET[$this->strPageVar] = $this->intPageTotal;
}
//当前页数
$this->intPage = $_GET[$this->strPageVar];
if($this->intPage < 1) $this->intPage = 1;
//本页开始的记录序号
$this->intBeginDate = ($this->intPage - 1) * $this->intNumOfPage;
//本页显示记录数
if ($this->intPage == $this->intPageTotal) {
$this->intNumOfDate = $this->intPageTotal - ($this->intPage - 1) * $this->intNumOfPage;
}else {
$this->intNumOfDate = $this->intNumOfPage;
}
//上一页
if ($this->intPage <= 1){
$this->intUpPage = NULL;
}else {
$this->intUpPage = $this->intPage - 1;
}
//下一页
if ($this->intPage >= $this->intPageTotal){
$this->intPageDown = NULL;
}else {
$this->intPageDown = $this->intPage + 1;
}
//翻页条最开始的页数
if ($this->intPage <= ceil($intNumOfBar/2)){
$this->intPageBegin = 1;
}else {
$this->intPageBegin = $this->intPage - ceil($intNumOfBar/2 - 1);
}
//翻页条最后的页数
$this->intPageEnd = $this->intPageBegin + $intNumOfBar - 1;
if ($this->intPageEnd >= $this->intPageTotal){
$this->intPageEnd = $this->intPageTotal;
$this->intPageBegin = $this->intPageTotal - $intNumOfBar + 1;
if($this->intPageBegin <= 0){
$this->intPageBegin = 1;
}
}
//地址栏
$this->get = $this->_get();
}
/*
* 解析辅助信息
*/
public function show() {
//$out = $this->page_text.$this->intPage;
$out = $this->intPage.$this->sign.$this->intPageTotal;
//$out .= $this->sign.$this->intPageTotal;
//$out .= $this->per_text.$this->intNumOfPage;
//$out .= $this->total_text.$this->intTotalDate."\r\n";
return $out;
}
/*
* 解析翻页条
*/
public function parse(){
$out = '';
$out.= $this->_href(1,'<span>'.$this->first_page.'</span>')."\r\n";
$out.= $this->_href($this->intUpPage,'<span>'.$this->pre_page.'</span>')."\r\n";
for ($i=$this->intPageBegin;$i<=$this->intPageEnd;$i++) {
$out.='[ '.$this->_href($i,$i).' ]'."\r\n";
}
$out.= $this->_href($this->intPageDown,'<span>'.$this->next_page.'</span></a>')."\r\n";
$out.= $this->_href($this->intPageTotal,'<span>'.$this->last_page.'</span>')."\r\n";
return $out;
}
/*
* 规范地址栏
*/
public function _get() {
$out = '?';
foreach ($_GET as $key => $value){
if ($key == $this->strPageVar) {
continue;
}
if($value){
$out .= $key.'='.htmlspecialchars($value).'&';
}
}
foreach ($_POST as $key => $value){
if ($key == $this->strPageVar) {
continue;
}
if($value && !is_array($value)){
$out .= $key.'='.htmlspecialchars($value).'&';
}
}
$out.=$this->strPageVar.'=';
return $out;
}
/*
* 链接样式
*/
public function _href($num, $content){
if ($num == '') {
return $content;
}elseif ($num == $this->intPage AND $content == $this->intPage) {
return '<font color="red"><strong>'.$content.'</strong></font>';
}elseif ($num == $this->intPageTotal AND $this->intPageTotal == $this->intPage) {
return $content;
}elseif ($num == 1 AND 1 == $this->intPage) {
return $content;
}else {
if(!empty($this->tag))
return '<a href="'.$_SERVER["SCRIPT_NAME"].$this->get.$num.'#'.$this->tag.'">'.$content.'</a>';
else
return '<a href="'.$_SERVER["SCRIPT_NAME"].$this->get.$num.'#huxing">'.$content.'</a>';
}
}
/*
*设置锚点
*/
public function set_tag($t){
$this->tag = $t;
}
/*
* 下拉菜单
*/
public function goto() {
$out = '<select name="select" onchange="javascript:window.location.href=this.options[this.selectedIndex].value">'."\r\n";
for ($i = 1;$i <= $this->intPageTotal;$i++){
$out .= $this->_option($i);
}
$out .= '</select>'."\r\n";
return $out;
}
public function _option($num) {
if ($this->intPage == $num) {
$selected = ' selected';
}else {
$selected = '';
}
return '<option value="'.$_SERVER["SCRIPT_NAME"].$this->get.$num.'"'.$selected.'>
'.$num.'</option>'."\r\n";
}
/*
* 获得菜单信息
*/
public function getPageInfo() {
$page['parse'] = $this->parse();
$page['show'] = $this->show();
$page['goto'] = $this->goto();
return $page;
}
}//End Class
?>