<?php
class Pages{
//数据源
var $sql;
//当前页的页码
var $currentPageNumber;
//每页的数据条数
var $itemsPerPage;
//调用的地址URL
var $url;
//总页数
var $totalPages;
//数据总数
var $totalNums;
//显示模块的前缀代码
var $model_block_pre;
//显示模块的后缀代码
var $model_block_end;
//显示循环数据的前缀代码
var $model_line_pre;
//显示循环数据的后缀代码
var $model_line_end;
//显示循环数据的主模版
var $model_main;
//模版参数
var $model_argvs;
//初始化函数
//$sql,数据源SQL
//$currentPageNumber,当前页的页码
//$url,调用的地址URL
//$model,用于显示的数据模版
//$itemsPerPage,每页的数据条数
function Pages($sql,$currentPageNumber,$url,$itemsPerPage = 10){
$this->sql = $sql;
$this->currentPageNumber = $currentPageNumber;
$this->itemsPerPage = $itemsPerPage;
$this->url = $url;
$this->url.=(stristr($this->url,'?')!=false)?'&':'?';
$this->get_total();
$this->get_safe_pages();
}
//获取总数和总页数
function get_total(){
$result = mysql_query($this->sql) or die("SQL Error:" . mysql_error());
$totalNums = mysql_num_rows($result);
$totalPages = ceil($totalNums/$this->itemsPerPage);
$this->totalNums = $totalNums;
$this->totalPages = $totalPages;
mysql_free_result($result);
}
//将当前页保持在安全的范围
function get_safe_pages(){
if ($this->currentPageNumber > $this->totalPages){
$this->currentPageNumber = $this->totalPages;
}elseif ($this->currentPageNumber < 1){
$this->currentPageNumber = 1;
}
}
//获取当前页的数据起始位置
function get_limit_start(){
$limit_start = ($this->currentPageNumber - 1) * $this->itemsPerPage;
return $limit_start;
}
//获取当前页的数据数目
function get_limit_nums(){
return $this->itemsPerPage;
}
//获取当前页的数据
function get_data(){
$SQL = $this->sql . " limit " . $this->get_limit_start() . "," . $this->get_limit_nums();
$result = mysql_query($SQL) or die("SQL Error:" . mysql_error());
return $result;
}
//检查主模版并且提取参数
function check_main_model(){
$temparray =split('\[',$this->model_main);
$returnarray = array();
for($i=1;$i<count($temparray);$i++){
$tempstr = substr($temparray[$i],0,stripos($temparray[$i],']'));
$templenth = strlen( $tempstr );
if ($templenth != 0){
$returnarray[] = $tempstr;
}
}
$this->model_argvs = $returnarray;
if ( count($returnarray) == 0 )return false;else return true;
}
//根据模版显示数据
function show_data($model){
$this->model_block_pre = $model[0];
$this->model_block_end = $model[1];
$this->model_line_pre = $model[2];
$this->model_line_end = $model[3];
$this->model_main = $model[4];
if ($this->totalNums < 1){
echo '没有数据';
}elseif ($this->check_main_model() == false){
echo '模版错误';
}else{
echo $this->model_block_pre;
$result = $this->get_data();
while ( $row = mysql_fetch_assoc($result) ) {
$tempstr = $this->model_main;
echo $this->model_line_pre;
for($i=0; $i < count($this->model_argvs);$i++){
$preg = "(.*)(\[". $this->model_argvs[$i] ."\])(.*)";
$target = "\\1". $row[$this->model_argvs[$i]] ."\\3";
$tempstr = ereg_replace($preg,$target,$tempstr);
}
echo $tempstr;
echo $this->model_line_end;
}
echo $this->model_block_end;
}
}
//显示分页项目
function show_pages(){
if ($this->totalNums < 1){
//没有数据
}else{
echo "<p>";
echo "<a href=\"".$this->url."page=1\">首页</a> ";
echo "<a href=\"".$this->url."page=". ($this->currentPageNumber - 1) ."\">上页</a> ";
echo "<a href=\"".$this->url."page=". ($this->currentPageNumber + 1) ."\">下页</a> ";
echo "<a href=\"".$this->url."page=". ($this->totalPages) ."\">尾页</a> ";
echo ($this->currentPageNumber) ."/". ($this->totalPages) ." ";
echo "每页". ($this->itemsPerPage) ."条/共". ($this->totalNums) ."条";
echo "</p>";
}
}
}
?>
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com