查出所有数据分页的类,模块分页类程序
分类:web前端

?phpPHP代码:

添加了几个自定义的 从mysql result 集合中 抽取指定片段的方法 , 没有调用释放的原因 这个涉及到 程序的 原来校验
也多亏网上大神的帮助啊 。。。。 老系统害死人啊, 后台都不能动
复制代码 代码如下:
/* 分页类
* @author xiaojiong & 290747680@qq.com
* @date 2011-08-17
*
* show(2) 1 ... 62 63 64 65 66 67 68 ... 150
* 分页样式
* #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}
*/
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;
public $plus = 3; //分页偏移量
protected $url;
public function get_page_result()
{
$lastResult = array();
$skipCount = $this->get_skip_row_count();
if(mysql_num_rows($result)>0)
{
mysql_data_seek($result,$skipCount);
}
$pageSize = $this->$list_rows;
while($row = mysql_fetch_array($result))
{
$pageSize --;
$lastResult[] = $row;
if($pageSize == 0)
{
break ;
}
}
return $lastResult;
}
public function get_skip_row_count()
{
return $this->list_rows*($this->now_page-1);
}
/**
* 构造函数
* @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->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 . '%20.%20')' . "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_2()
{
if($this->total_pages != 1)
{
$return = '';
$return .= $this->up_page('for($i = 1;$i<=$this->total_pages;$i++)
{
if($i == $this->now_page)
{
$return .= "$in";
}
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;
}
}
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 .= "$in";
}
else
{
$return .= $this->_get_link($i, $i) . "n";
}
}
$return .= $this->down_page();
$return .= $this->last_page();
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;
}
}
?>

 代码如下

?php$page_info = count_page($total , PER_PAGE );//分页参数$smarty-assign('page_first' , $page_info['page_first']);$smarty-assign('page_last' , $page_info['page_last']);$smarty-assign('page_str' , $page_info['page_str']);$sql = SELECT * FROM `news` ORDER BY `id` DESC LIMIT {$page_info['offset']} , . PER_PAGE;?

<style type="text/css">
<!--
body {
 width:800px;
 margin:0 auto;
 margin-top:50px;
 font-size:12px;
}
a {
 color:#014ccc;
 text-decoration:none;
}
.div {
 float:left;
 cursor:pointer;
 font-weight:bold;
 margin-right:5px;
 display: block;
 padding:3px 7px;
 text-align:center;
 border:#bbdded solid 1px;
}
.divs {
 float:left;
 font-weight:bold;
 margin-right:5px;
 display: block;
 padding:3px 7px;
 text-align:center;
 border:#cccccc solid 1px;
}
.divsi {
 float:left;
 font-weight:bold;
 margin-right:5px;
 display: block;
 padding:3px 7px;
 text-align:center;
 background:#3399ff;
 color:#ffffff;
 border:#cccccc solid 1px;
}
.div:hover {
 background:#3399ff;
 color:#ffffff;
}
.divsi:hover {
 background:#3399ff;
 color:#ffffff;
}
-->

//PAGE : $total总页数 $per每页显示条数 $show 列表条数function count_page($total = 0 , $per ){$page_out = array( 'page_first' = '', //首页 'page_last' = '', //末页 'page_str' = '' , //分页 'now_page' = 1 , //当前页 'offset' = 0, //偏移量 );$page_count = ceil($total / $per); //总页数if($page_count = 1){ return $page_out;}$page_out['now_page'] = @$_GET['page'] + 0;if($page_out['now_page'] 1){ $page_out['now_page'] = 1;} elseif($page_out['now_page'] $page_count){ $page_out['now_page'] = $page_count;}

</style>

$page_out['offset'] = ($page_out['now_page'] - 1) * $per;//URL正则$SELF = [url={$_SERVER[]{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF'[/url]]}?;@$QUERY = $_SERVER['QUERY_STRING'];$query_page = preg_replace(array(!((|^)page=([^]+))|(page=)! , !(^)|[]{2,}|([]$)!) , array('' , '') , $QUERY);$query_page = $SELF . $query_page . ($query_page ? : );/*first page*/$page_out['page_first'] = '1';if($page_out['page_first'] == $page_out['now_page']){ $page_out['page_first'] = 'li class=previous-off首页/li';}else{ $page_out['page_first'] = 'lia href=?page=1首页/a/li';}/*last page*/$page_out['page_last'] = $page_count;if($page_out['page_last'] == $page_out['now_page']){ $page_out['page_last'] = 'li class=next-off尾页/li';}else{ $page_out['page_last'] = 'lia href=?page=' . $page_count. '尾页/a/li';}//限制分页 2008-6-2 Edit$pageleft = $page_out['now_page'] = 4 ? $page_out['now_page'] - 4 : 1;$pageright = $page_out['now_page']

 代码如下

  • 4 = $page_count ? $page_out['now_page'] + 4 : $page_count;

<?php
mysql_connect('localhost','root','');
mysql_select_db('news');
mysql_query('set names gbk');

for($i = $pageleft; $i = $pageright; $i++) { if($i == $page_out['now_page']){ $page_out['page_str'] .= 'li class=active' . $i . '/li'; } else { $page_out['page_str'] .= 'lia href=' . $query_page . 'page=' . $i . '' . $i . '/a/li'; }}return $page_out;}?

$sql = mysql_query("select * from `v_char`");
$num = mysql_num_rows($sql);
include 'page.class.php';
/*参数说明:
 *getpages($pagesize,$pagenum,$page,$init,$page_len);
 *$pagesize 每页记录
 *$pagenum 总记录数
 *$page $_get[page]
 *$init 数字分页最小数,默认为1,一般不用更改。
 *$page_len 数字分页的数量。
*/
$model = new getpages(10,$num,$_get['page']);

复制PHP内容到剪贴板 PHP代码:CSS 文件

$sql = "select * from `v_char` limit $model->offset,$model->pagesize";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res))
{
 echo $row['id'] . " - " . $row['name'] .'<br>';
}

/*PAGE*/#page li { border:0; margin:0; padding:0; font-size:11px; list-style:none; /* savers */ float:left; }#page a { border:solid 1px #9aafe5; margin-right:2px; }#page .previous-off,#page .next-off { border:solid 1px #DEDEDE; color:#888888; display:block; float:left; font-weight:bold; margin-right:2px; padding:3px 4px; }#page .next a,#page .previous a { font-weight:bold; }#page .active { background:#2e6ab1; color:#FFFFFF; font-weight:bold; display:block; float:left; padding:4px 6px; /* savers */ margin-right:2px; }#page a:link,#page a:visited { color:#0e509e; display:block; float:left; padding:3px 6px; text-decoration:none; }#page a:hover{ border:solid 1px #0e509e; }

//输出分页信息
echo $model->getpage();
?>

page.class.php

<?php
/*
 * author:陈凯
 * data:2010-09-14
 */
class getpages
{
 public $pagesize;  //每页记录
 public $offset;    //起始记录数
 public $pagenum;   //总记录
 public $page;      //当前页码
 public $init;      //数字分页最左边数字
 public $page_len;  //数字分页最右边数字
 public $pages;     //总页数
 
 //初始化赋值。
 function __construct($pagesize,$pagenum,$page,$init=1,$page_len=7)
 {
  $this->pagesize = $pagesize;//每页记录
  $this->pagenum  = $pagenum; //总记录
  $this->page     = $page;
  $this->pages    = ceil($this->pagenum/$this->pagesize); //总页数
  if(empty($this->page) || $this->page <1 || $this->page > $this->pages)$this->page=1;
  $this->init     = $init; //数字分页左
  $this->page_len = $page_len;//数字分页右
  $this->offset   = ($this->page-1)*$this->pagesize;
 }
 
 //输出分页
 function getpage()
 {
  $this->prepg = $this->page-1;  //上一页
  $this->nextpg= ($this->page == $this->pages) ? 0 : $this->page+1;  //下一页
//   if($this->pages == 1) return false;  //如果只有一页跳出
  $pagenav = "<span class='divs'>共 ".$this->pages."页".$this->pagenum."条</span>";
   if($this->page == 1)
   {
    $pagenav .="<a href='#' class='div'>首页</a> ";
    $pagenav .="<a href='#' class='div'>上一页</a> ";
   }
   else
   {
     $pagenav .="<a href='".$this->get_url()."=1' class='div'>首页</a> ";
    $pagenav .="<a href='".$this->get_url()."=".$this->prepg."' class='div'>上一页</a> ";  
   }
   
   $pagenav .= $this->getvalpage(); //数字分页
   
   if($this->page == $this->pages)
   {
     $pagenav .="<a href='#' class='div'>下一页</a> ";
    $pagenav .="<a href='#' class='div'>尾页</a> "; 
   }
   else
   {
      $pagenav .="<a href='".$this->get_url()."=".$this->nextpg."' class='div'>下一页</a> ";
    $pagenav .="<a href='".$this->get_url()."=".$this->pages."' class='div'>尾页</a> ";
   }

 

 

   
   //下拉菜单分页列表  

 

 代码如下

 $pagenav .= "<select name='page' class='select' onchange='window.location.href="".$this->get_url()."="+this.value'> ";
   
   for($i=1;$i<=$this->pages;$i++)
   {
    if($i == $this->page){
     $pagenav .= "<option value='".$i."' selected>".$i."</option> ";
    }
    else
    {
     $pagenav .= "<option value='".$i."'>".$i."</option> ";
    }
   }
   $pagenav.="</select> ";
   
   return $pagenav;
 }
 
 //数字分页
 function getvalpage()
 {
  $max_p = $this->pages;
  $this->page_len  = ($this->page_len%2) ? $this->page_len : $this->page_len+1; //如果为偶数便+1
  $pageoffset = ($this->page_len-1)/2; //数字分页左右数量
  
  //总页数和数字分页显示数
  if($this->pages > $this->page_len)
  {
   //数字分页左右数量大于等于当前页面,不进行偏移
   if($pageoffset>=$this->page)
   {
    $this->init = 1;
    $max_p= $this->page_len;
   }
   else
   {
    //当前页码+左右数量 大于总数量,不进行偏移
    if($this->page+$pageoffset > $this->pages)
    {
     $this->init  = $this->pages-$this->page_len+1;
    }
    else
    {
     //进行偏移,最左边 当前页码 - 左右数量
     $this->init   = $this->page-$pageoffset;
     $max_p = $this->page+$pageoffset;
    }
   }
  }
  
  //循环数字分页
  for($i=$this->init ;$i<=$max_p;$i++)
  {
   //$i 等于 当前页码
   if($i == $this->page)
   {
    $pagenav .= "<a href='#' class='divsi'>".$i."</a>";
   }
   else
   {
    $pagenav .= "<a href='".$this->get_url()."=".$i."' class='div'>".$i."</a>";
   }
  }
  return $pagenav;
 }
 
 //获取url地址
 function get_url()
 {
  $url = $_server['request_uri'];
  $parse_url = parse_url($url);
  $query_url = $parse_url['query']; //取出url上的参数

  if($query_url)
  {
   //将&page 替换为空
   $query_url = ereg_replace("(^|&)page=$this->page","",$query_url);
   $url = str_replace($parse_url['query'],$query_url,$url);
   if($query_url)$url .= '&page';
   else $url .= 'page';
  }
  else
  {
   $url .= '?page';
  }
  return $url;
 }
}

?>

数据库教程

/*
navicat mysql data transfer

source server         : localhost_3306
source server version : 50051
source host           : localhost:3306
source database       : news

target server type    : mysql
target server version : 50051
file encoding         : 65001

date: 2010-03-30 00:17:08
*/

set foreign_key_checks=0;


-- table structure for `v_char`


drop table if exists `v_char`;
create table `v_char` (
  `id` int(10) not null auto_increment,
  `name` char(10) default null,
  primary key  (`id`)
) engine=innodb auto_increment=401 default charset=gbk;


-- records of v_char


insert into `v_char` values ('1', 'test1');
insert into `v_char` values ('2', 'test2');
insert into `v_char` values ('3', 'test3');
insert into `v_char` values ('4', 'test4');
insert into `v_char` values ('5', 'test5');
insert into `v_char` values ('6', 'test6');
insert into `v_char` values ('7', 'test7');
insert into `v_char` values ('8', 'test8');
insert into `v_char` values ('9', 'test9');
insert into `v_char` values ('10', 'test10');
insert into `v_char` values ('11', 'test11');
insert into `v_char` values ('12', 'test12');

本文由10bet手机官网发布于web前端,转载请注明出处:查出所有数据分页的类,模块分页类程序

上一篇:一条sql语句搞定数据库分页 下一篇:没有了
猜你喜欢
热门排行
精彩图文