php 远程分页类
page_total_rows - 每页展示数量 默认值20
$total_rows - 总计数据条目数
$totpages - 总页数计算
$pages_current - 当前页面
利用url参数传递 当前页码 url参数名称 pages
$style - 页码展示样式可以通过外部访问样式属性进行修改
***********************使用方法**********************
//调用该类 $pages = new pages; //调用该类后请修改数据集总条数 $pages->total_rows = $totrows; //$pages->main();方法将返回limit需要的2个参数 关联数组的a,b2个元素 $limit = $pages->main();
通过访问不同方法即可展示不同的功能!
*/
<?php
class pages {
public $page_total_rows = 20; //每页展示数量
public $total_rows; //总计数据条目数
public $totpages; //总页数
public $current_url; //当前页面名称
private $ask; //是否出现问号
public $style = '<style type="text/css教程">
.pages_norename{width:50px; height:20px; float:left; background-color:#e3eff3;
margin-right:5px; text-align:center; line-height:20px; border:1px solid #333333;}
.pages_norename a{display:block; width:50px; height:20px; color:#333333; text-decoration:none;}
.pages_norename a:hover{background-color:#ff9900; color:#ffffff;}
.pages_nore_more{width:auto; height:20px; float:left; margin-right:5px; line-height:20px;
background-color:#e3eff3; border:1px solid #333333;}
.pages_nore_more a{display:block; width:20px; height:20px; color:#333333;
text-decoration:none; text-align:center;}
.pages_nore_more a:hover{background-color:#ff9900; color:#ffffff;}
.pages_se{width:auto; height:20px; float:left;}
.pages_se select{margin:0px; padding:0px; font-family:arial,
helvetica, sans-serif; font-size:12px;}
</style>
';
//核心计算 并以数组的形式返回查询sql 语句的必须值 limit a,b;
function main() {
$this->totpages = ceil($this->total_rows / $this->page_total_rows); //总页数计算
//获得当前页码-------------------
if (!isset($_get['pages'])) {
$this->pages_current = 1;
} else {
$this->pages_current = intval($_get['pages']);
//判断页面不为0
if ($this->pages_current < 1) {
$this->pages_current = 1;
}
//判断页面不能大于最大页码数量
if ($this->pages_current > $this->totpages) {
$this->pages_current = $this->totpages;
}
//注销url 参数 pages 和 total_rows 为了更好的传递其他url参数
if (isset($_get['pages'])) {
unset($_get['pages']);
}
if (isset($_get['total_rows'])) {
unset($_get['total_rows']);
}
}
//获得当前页码--------------------
$limit['a'] = $start = ($this->pages_current - 1) * $this->page_total_rows;
$limit['b'] = $this->page_total_rows;
//获得当前页面名称
$urlin = explode('/', $_server['php教程_self']);
$tot_url = sizeof($urlin);
$this->current_url = $urlin[$tot_url - 1];
//获得当前页面传递的url
if (sizeof($_get) > 0) {
foreach ($_get as $key => $values) {
$urlsget.= $key.'='.$values.'&';
}
$this->current_url.= '?'.$urlsget;
$this->ask = '';
} else {
$this->ask = '?';
}
//输出样式
echo $this->style;
return $limit;
}
//展示分页
//1 第一页
function firstpage() {
echo '<div class="pages_norename"><a href="'.$this->current_url.'">首页</a></div>';
}
//2 上一页
function prepage() {
echo '<div class="pages_norename"><a href="'.$this->current_url.$this->ask.'pages='.($this->pages_current - 1).'">上一页</a></div>';
}
//3 下一页
function nextpage() {
echo '<div class="pages_norename"><a href="'.$this->current_url.$this->ask.'pages='.($this->pages_current + 1).'">下一页</a></div>';
}
//4 最后一页
function lastpage() {
echo '<div class="pages_norename"><a href="'.$this->current_url.$this->ask.'pages='.($this->totpages).'">尾页</a></div>';
}
//中间过渡页
function morepage() {
if ($this->pages_current == 1) {
$newtj = $this->pages_current + 9;
} elseif ($this->pages_current == 2) {
$newtj = $this->pages_current + 8;
} elseif ($this->pages_current == 3) {
$newtj = $this->pages_current + 7;
} else {
$newtj = $this->pages_current + 6;
}
for ($i = $this->pages_current - 3; $i <= $newtj; $i++) {
if ($i == $this->pages_current) {
$strong = '<strong>';
$strong2 = '</strong>';
} else {
$strong = '';
$strong2 = '';
}
if ($i >= 1) {
echo '<div class="pages_nore_more"><a href="'.$this->current_url.$this->ask.'pages='.$i.'">'.$strong.$i.$strong2.'</a></div>';
}
if ($i >= $this->totpages) {
break;
}
}
}
//跳转页面
function changepage() {
echo '<div class="pages_se"><select name="dd">';
for ($i = 1; $i <= $this->totpages; $i++) {
if ($this->pages_current == $i) {
$selected = ' selected="selected"';
} else {
$selected = '';
}
echo '<option value="'.$i.'"'.$selected.'>第'.$i.'页</option>';
}
echo '</select></div>';
}
}
?>该类可以自动识别 url 参数 避免了一般分页类 丢失url参数问题
样式 可以通过style属性 进行修改
提供 首页 上一页 下一页 尾页 中间 过渡页 跳转菜单功能
教程网址:http://www.phprm.com/code/33690.html
欢迎收藏∩_∩但请保留本文链接。