分页样式php,PHP分页类,有四种样式共选择

[php]代码库查询时用到的:

$pagesize=20; //每页显示数量

if($_POST["pageid"]){

if($_POST["form1"]){

$pageval=1;

}else{

$pageval=$_POST["pageid"];

}

$pageid=($pageval-1)*$pagesize;

$pageid.=',';

}

if($_GET["p"]){

$pageval=$_GET["p"];

$pageid=($pageval-1)*$pagesize;

$pageid.=',';

}

sql:

$sql="select * from table order by addtime desc limit $pageid $pagesize";

使用方法:

//分页类调用

require("inc/page.class.php");

$params = array(

'total_rows'=>$Num, //信息总数

'method'=>'get', //方法

'ajax_func_name'=>'goToPage',

'now_page'=>$pageval, //当前页

'list_rows'=>$pagesize, //分页显示条数

);

$page = new Core_Lib_Page($params);

echo $page->show(2);

?>

page.class.php源代码:(打包要积分下载)

/**

* PHP分页类

* 有四种分页样式:可调用show(1);show(2);show(3);show(4);查看

* 分页样式可灵活使用自定义多种样式

*/

class Core_Lib_Page

{

public $first_row; //起始行数

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

protected $total_pages; //总页数

protected $total_rows; //总行数

protected $now_page; //当前页数

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

protected $parameter = '';

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

protected $ajax_func_name; //ajax分页下的方法名称

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

protected $url;

/**

* 构造函数

* @param unknown_type $data

*/

public function __construct($data = array())

{

$this->total_rows = $data['total_rows'];

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

$this->list_rows = !empty($data['list_rows']) && $data['list_rows'] <= 100 ? $data['list_rows'] : 15;

$this->total_pages = ceil($this->total_rows / $this->list_rows);

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

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

$this->getsession = $data['getsession'];

$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]) ? intval($_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);

}

/**

* 得到当前连接

* @param $page

* @param $text

* @return string

*/

protected function _get_link($page,$text)

{

switch ($this->method) {

case 'ajax':

$parameter = '';

if($this->parameter)

{

$parameter = ','.$this->parameter;

}

return '' . $text . '' . "\n";

break;

case 'html':

$url = str_replace('?', $page,$this->parameter);

return '' . $text . '' . "\n";

break;

default:

return '' . $text . '' . "\n";

break;

}

}

/**

* 设置当前页面链接

*/

protected 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;

}

/**

* 得到$page的url

* @param $page 页面

* @return string

*/

protected 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 '';

}

/**

* 分页样式输出

* @param $param

* @return string

*/

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 '';

}

protected 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 .= "$i\n";

}

else

{

$return .= $this->_get_link($i, $i) . "\n";

}

}

$return .= $this->down_page();

$return .= $this->last_page();

return $return;

}

protected function show_2()

{

if($this->total_pages != 1)

{

$return = '';

$return .= '

 ' .$this->total_pages. ' 页  当前第  ' .$this->now_page. ' 页, 共有记录  ' .$this->total_rows. ' 
';

$return .= $this->up_page('上一页');

for($i = 1;$i<=$this->total_pages;$i++)

{

if($i == $this->now_page)

{

$return .= "$i\n";

}

else

{

if($this->now_page-$i>=4 && $i != 1)

{

$return .="...\n";

$i = $this->now_page-3;

}

else

{

if($i >= $this->now_page+5 && $i != $this->total_pages)

{

$return .="...\n";

$i = $this->total_pages;

}

$return .= $this->_get_link($i, $i) . "\n";

}

}

}

$return .= $this->down_page('下一页');

$return .= '

';

return $return;

}

}

protected function show_3()

{

$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 = '总计 ' .$this->total_rows. ' 个记录分为 ' .$this->total_pages. ' 页, 当前第 ' . $this->now_page . ' 页 ';

$return .= ',每页 ';

$return .= ' ';

$return .= $this->first_page()."\n";

$return .= $this->up_page()."\n";

$return .= $this->down_page()."\n";

$return .= $this->last_page()."\n";

$return .= '';

for ($i = $begin;$i<=$begin+10;$i++)

{

if($i>$this->total_pages)

{

break;

}

if($i == $this->now_page)

{

$return .= ''.$i.'';

}

else

{

$return .= '' .$i. '';

}

}

$return .= '';

return $return;

}

protected function show_4()

{

$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->total_pages. ' 页  当前第  ' .$this->now_page. ' 页, 共有记录  ' .$this->total_rows. ' 
';

$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 .= "$i\n";

}

else

{

$return .= $this->_get_link($i, $i) . "\n";

}

}

$return .= $this->down_page();

$return .= $this->last_page();

$return .= '

';

return $return;

}

}

?>

[源代码打包下载]

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值