您的位置:首页 > 编程语言 > PHP开发

根据网上代码 自己改的PHP分页类,不含ajax无刷新分页

2013-04-14 16:55 766 查看
PHP 分页类叫 page.class.php

<?php
class pageTest
{
public   $first_row;           //起始行数

public   $list_rows;        //列表每页显示行数

public   $total_pages;      //总页数

public   $total_rows;          //总行数

public   $now_page;         //当前页数

public   $method  = 'defalut'; //处理情况 Ajax分页 Html分页(静态化时) 普通get方式

public   $parameter = '';

public   $page_name;        //分页参数的名称

//public    $ajax_func_name;

public        $plus = 3;         //分页偏移量

public  $url;

/**
* 构造函数
* @param unknown_type $data
*/

public function __construct($data = array()){

$this->total_rows = $data['total_rows']; //总行数

$this->list_rows         = !empty($data['list_rows']) && $data['list_rows'] <= 100 ? $data['list_rows'] : 15;
//每页显示行数 默认为15行
$this->total_pages        = ceil($this->total_rows / $this->list_rows);
//总页数
$this->parameter         = !empty($data['parameter']) ? $data['parameter'] : '';  //传递过来的参数

$this->page_name          = !empty($data['page_name']) ? $data['page_name'] : 'p';  //传递参数的命名

$this->method           = !empty($data['method']) ? $data['method'] : '';

//当前页面
if(!empty($data['now_page'])){

$this->now_page = intval($data['now_page']);

}else{

$this->now_page = !empty($_GET[$this->page_name]) ? $_GET[$this->page_name] : 1;

}

$this->now_page = $this->now_page <= 0 ? 1 : $this->now_page;

if(!empty($this->total_pages) && ($this->now_page > $this->total_pages)){

$this->now_page = $this->total_pages;

}

$this->first_row = $this->list_rows * ($this->now_page-1);

}
/* 测试功能的函数*/
public function aa(){

echo $_GET[$this->page_name];

}
public function _get_link($page,$text){

return '<a href="' . $this->_get_url($page) . '">' . $text . '</a>' . "\n";

}

public function _set_url()
{
$url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;

$parse = parse_url($url);

if(isset($parse['query'])) {
parse_str($parse['query'],$params);

unset($params[$this->page_name]);

$url   =  $parse['path'].'?'.http_build_query($params);

}
if(!empty($params))
{
$url .= '&';
}
$this->url = $url;

}
public function _get_url($page)
{
if($this->url === NULL)
{
$this->_set_url();
}
//    $lable = strpos('&', $this->url) === FALSE ? '' : '&';
return $this->url . $this->page_name . '=' . $page;
}

/**
* 得到第一页
* @return string
*/
public function first_page($name = '第一页')
{
if($this->now_page > 5)
{
return $this->_get_link('1', $name);
}
return '';
}

/**
* 最后一页
* @param $name
* @return string
*/
public function last_page($name = '最后一页')
{
if($this->now_page < $this->total_pages - 5)
{
return $this->_get_link($this->total_pages, $name);
}
return '';
}

/**
* 上一页
* @return string
*/
public function up_page($name = '上一页')
{
if($this->now_page != 1)
{
return $this->_get_link($this->now_page - 1, $name);
}
return '';
}

/**
* 下一页
* @return string
*/
public function down_page($name = '下一页')
{
if($this->now_page < $this->total_pages)
{
return $this->_get_link($this->now_page + 1, $name);
}
return '';
}

public function show($param = 1)
{
if($this->total_rows < 1)
{
return '';
}

$className = 'show_' . $param;

$classNames = get_class_methods($this);

if(in_array($className, $classNames))
{
return $this->$className();
}
return '';
}

public function show_1()
{
$plus = $this->plus;
if( $plus + $this->now_page > $this->total_pages)
{
$begin = $this->total_pages - $plus * 2;
}else{
$begin = $this->now_page - $plus;
}

$begin = ($begin >= 1) ? $begin : 1;
$return = '';
$return .= $this->first_page();
$return .= $this->up_page();
for ($i = $begin; $i <= $begin + $plus * 2;$i++)
{
if($i>$this->total_pages)
{
break;
}
if($i == $this->now_page)
{
$return .= "<a class='now_page'>$i</a>\n";
}
else
{
$return .= $this->_get_link($i, $i) . "\n";
}
}
$return .= $this->down_page();
$return .= $this->last_page();
return $return;
}
}
?>


使用方法 我介绍的是在.php文件中的使用方法,因为本人本地 没有用html 与PHP 分离

代码是:

/*首先肯定是连接数据库,数据库类的代码我就不发了。*/

require_once("Page.class.php");
$tid='111';
$params = array(
'total_rows'=>10, #(必须)
'method'    =>'', #(必须)
'parameter' =>'tid='.$tid,  #(看情况,这个是传输的参数)
'now_page'  =>$_GET['p'],  #(必须)
'list_rows' =>2, #(可选) 默认为15
);

$page = new pageTest($params);

$sql  = "select title from dede_archives where 1 limit ".$page->first_row.",".$page->list_rows."";

$query = mysql_query($sql);

$arr1 = array();
while($arr = mysql_fetch_array($query)){

echo  "<br>".$arr[title]."<br>";
}

?>
<div id="page">
<?php echo $page->show(1);
echo "<br>";

?></div>
<style type="text/css">
#page{font:12px/16px arial}
#page span{float:left;margin:0px 3px;}
#page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px 7px; text-decoration:none;color:#666}
#page a.now_page,#page a:hover{color:#fff;background:#05c}
</style>

//style 的是分页样式
//PHP 部分是 使用方法
//html 部分是 显示时用到的DIV
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: