设为首页收藏本站

就爱编程论坛

 找回密码
 注册

人人连接登陆

无需注册,直接登录

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 431|回复: 2
打印 上一主题 下一主题

PHP分页类实例收集 [复制链接]

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    无聊
    2025-5-27 03:37:20
  • 签到天数: 366 天

    [LV.9]以坛为家II

    论坛先锋 学习至圣 荣誉成员 论坛元老 活跃之星 终极领袖

    我玩的应用:

    跳转到指定楼层
    楼主
    发表于 2011-8-26 12:44:54 |只看该作者 |倒序浏览
    1. <?php
    2. /**
    3. * 分页类
    4. * @author 290747680@qq.com
    5. * @date 2011-08-17
    6. * @notice
    7. * $data['ajax_func_name'] = 'page';
    8. * $data['is_ajax'] = true;
    9. * $data['parameter'] = "'a','b'";
    10. *
    11. *
    12. * show(2) 1 ... 62 63 64 65 66 67 68 ... 150
    13. * 分页样式
    14. * #page{font:12px/16px arial}
    15. * #page span{float:left;margin:0px 3px;}
    16. * #page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px 7px; text-decoration:none;color:#666}
    17. * #page a.now_page,#page a:hover{color:#fff;background:#05c}
    18. */

    19. class page
    20. {
    21. public $first_row; //起始行数

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

    23. protected $total_pages; //总页数

    24. protected $total_rows; //总行数

    25. protected $now_page; //当前页数

    26. protected $is_ajax = FALSE; //是否ajax

    27. protected $parameter = '';

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

    29. protected $ajax_func_name;

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

    31. protected $url;


    32. /**
    33. * 构造函数
    34. * @param unknown_type $data
    35. */
    36. public function __construct($data = array())
    37. {
    38. $this->total_rows = $data['total_rows'];

    39. $this->parameter = !empty($data['parameter']) ? $data['parameter'] : '';
    40. $this->list_rows = !empty($data['list_rows']) ? $data['list_rows'] : 10;
    41. $this->total_pages= ceil($this->total_rows / $this->list_rows);
    42. $this->page_name = !empty($data['page_name']) ? $data['page_name'] : 'p';

    43. /* 当前页面 */
    44. if(!empty($data['now_page']))
    45. {
    46. $this->now_page = intval($data['now_page']);
    47. }else{
    48. $this->now_page = !empty($_GET[$this->page_name]) ? intval($_GET[$this->page_name]):1;
    49. }
    50. $this->now_page = $this->now_page <= 0 ? 1 : $this->now_page;


    51. if(!empty($data['is_ajax']))
    52. {
    53. $this->is_ajax = TRUE;
    54. $this->ajax_func_name = $data['ajax_func_name'];
    55. }

    56. if(!empty($this->total_pages) && $this->now_page > $this->total_pages)
    57. {
    58. $this->now_page = $this->total_pages;
    59. }
    60. $this->first_row = $this->list_rows * ($this->now_page - 1);
    61. }

    62. /**
    63. * 得到当前连接
    64. * @param $page
    65. * @param $text
    66. * @return string
    67. */
    68. protected function _get_link($page,$text)
    69. {
    70. if($this->is_ajax)
    71. {
    72. $parameter = '';
    73. if($this->parameter)
    74. {
    75. $parameter = ','.$this->parameter;
    76. }
    77. return '<a onclick="' . $this->ajax_func_name . '(\'' . $page . '\''.$parameter.')" href="javascript:void(0)">' . $text . '</a>' . "\n";
    78. }
    79. else
    80. {
    81. return '<a href="' . $this->_get_url($page) . '">' . $text . '</a>' . "\n";
    82. }
    83. }


    84. /**
    85. * 设置当前页面链接
    86. */
    87. protected function _set_url()
    88. {
    89. $url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
    90. $parse = parse_url($url);
    91. if(isset($parse['query'])) {
    92. parse_str($parse['query'],$params);
    93. unset($params[$this->page_name]);
    94. $url = $parse['path'].'?'.http_build_query($params);
    95. }
    96. if(!empty($params))
    97. {
    98. $url .= '&';
    99. }
    100. $this->url = $url;
    101. }

    102. /**
    103. * 得到$page的url
    104. * @param $page 页面
    105. * @return string
    106. */
    107. protected function _get_url($page)
    108. {
    109. if($this->url === NULL)
    110. {
    111. $this->_set_url();
    112. }
    113. // $lable = strpos('&', $this->url) === FALSE ? '' : '&';
    114. return $this->url . $this->page_name . '=' . $page;
    115. }


    116. /**
    117. * 得到第一页
    118. * @return string
    119. */
    120. public function first_page($name = '第一页')
    121. {
    122. if($this->now_page > 5)
    123. {
    124. return $this->_get_link('1', $name);
    125. }
    126. return '';
    127. }

    128. /**
    129. * 最后一页
    130. * @param $name
    131. * @return string
    132. */
    133. public function last_page($name = '最后一页')
    134. {
    135. if($this->now_page < $this->total_pages - 5)
    136. {
    137. return $this->_get_link($this->total_pages, $name);
    138. }
    139. return '';
    140. }

    141. /**
    142. * 上一页
    143. * @return string
    144. */
    145. public function up_page($name = '上一页')
    146. {
    147. if($this->now_page != 1)
    148. {
    149. return $this->_get_link($this->now_page - 1, $name);
    150. }
    151. return '';
    152. }

    153. /**
    154. * 下一页
    155. * @return string
    156. */
    157. public function down_page($name = '下一页')
    158. {
    159. if($this->now_page < $this->total_pages)
    160. {
    161. return $this->_get_link($this->now_page + 1, $name);
    162. }
    163. return '';
    164. }

    165. /**
    166. * 分页样式输出
    167. * @param $param
    168. * @return string
    169. */
    170. public function show($param = 1)
    171. {
    172. switch ($param) {
    173. case 1:
    174. return $this->show_1();
    175. break;

    176. case 2:
    177. return $this->show_2();
    178. break;
    179. }
    180. }

    181. protected function show_2()
    182. {
    183. if($this->total_pages != 1)
    184. {
    185. $return = '';
    186. $return .= $this->up_page('<');
    187. for($i = 1;$i<=$this->total_pages;$i++)
    188. {
    189. if($i == $this->now_page)
    190. {
    191. $return .= "<a class='now_page'>$i</a>\n";
    192. }
    193. else
    194. {
    195. if($this->now_page-$i>=4 && $i != 1)
    196. {
    197. $return .="<span class='pageMore'>...</span>\n";
    198. $i = $this->now_page-3;
    199. }
    200. else
    201. {
    202. if($i >= $this->now_page+5 && $i != $this->total_pages)
    203. {
    204. $return .="<span>...</span>\n";
    205. $i = $this->total_pages;
    206. }
    207. $return .= $this->_get_link($i, $i) . "\n";
    208. }
    209. }
    210. }
    211. $return .= $this->down_page('>');
    212. return $return;
    213. }
    214. }

    215. protected function show_1()
    216. {
    217. $plus = $this->plus;
    218. if( $plus + $this->now_page > $this->total_pages)
    219. {
    220. $begin = $this->total_pages - $plus * 2;
    221. }else{
    222. $begin = $this->now_page - $plus;
    223. }

    224. $begin = ($begin >= 1) ? $begin : 1;
    225. $return = '';
    226. $return .= $this->first_page();
    227. $return .= $this->up_page();
    228. for ($i = $begin; $i <= $begin + $plus * 2;$i++)
    229. {
    230. if($i == $this->now_page)
    231. {
    232. $return .= "<a class='now_page'>$i</a>\n";
    233. }
    234. else
    235. {
    236. $return .= $this->_get_link($i, $i) . "\n";
    237. }
    238. }
    239. $return .= $this->down_page();
    240. $return .= $this->last_page();
    241. return $return;
    242. }
    243. }

    244. ?>
    复制代码
    调用示例
    1. <?php
    2. $totalrows=1600;
    3. $pagesize=30;

    4. $page=new page(
    5. array(
    6. "total_rows"=>$totalrows,
    7. "parameter"=>"act=list",
    8. "list_rows"=>$pagesize,
    9. "page_name "=>"page"
    10. )
    11. );
    12. echo $page->show();
    13. ?>
    复制代码
    分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    分享分享0 收藏收藏0 支持支持0 反对反对0 分享到人人 转发到微博
    [img=http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=fRUcHhYWGAQ9GxIFEBwUEVMeEhA]http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png[/img]

    使用道具 举报

    Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    无聊
    2025-5-27 03:37:20
  • 签到天数: 366 天

    [LV.9]以坛为家II

    论坛先锋 学习至圣 荣誉成员 论坛元老 活跃之星 终极领袖

    沙发
    发表于 2011-8-26 12:51:27 |只看该作者
    1. <?php
    2. /**
    3. * filename: ext_page.class.php
    4. * @package:phpbean
    5. * @author :feifengxlq<feifengxlq#gmail.com>
    6. * @copyright :Copyright 2006 feifengxlq
    7. * @license:version 2.0
    8. * @create:2006-5-31
    9. * @modify:2006-6-1
    10. * @modify:feifengxlq 2006-11-4
    11. * description:超强分页类,四种分页模式,默认采用类似baidu,google的分页风格。
    12. * 2.0增加功能:支持自定义风格,自定义样式,同时支持PHP4和PHP5,
    13. * to see detail,please visit http://www.phpobject.net/blog/read.php
    14. * example:
    15. * 模式四种分页模式:
    16.    require_once('../libs/classes/page.class.php');
    17.    $page=new page(array('total'=>1000,'perpage'=>20));
    18.    echo 'mode:1<br>'.$page->show();
    19.    echo '<hr>mode:2<br>'.$page->show(2);
    20.    echo '<hr>mode:3<br>'.$page->show(3);
    21.    echo '<hr>mode:4<br>'.$page->show(4);
    22.    开启AJAX:
    23.    $ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
    24.    echo 'mode:1<br>'.$ajaxpage->show();
    25.    采用继承自定义分页显示模式:
    26.    demo:[url=http://www.phpobject.net/blog]http://www.phpobject.net/blog[/url]
    27. */
    28. class page
    29. {
    30. /**
    31.   * config ,public
    32.   */
    33. var $page_name="page";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page
    34. var $next_page='>';//下一页
    35. var $pre_page='<';//上一页
    36. var $first_page='First';//首页
    37. var $last_page='Last';//尾页
    38. var $pre_bar='<<';//上一分页条
    39. var $next_bar='>>';//下一分页条
    40. var $format_left='[';
    41. var $format_right=']';
    42. var $is_ajax=false;//是否支持AJAX分页模式
    43. var $selectname='pageselect'; //select名称
    44. /**
    45.   * private
    46.   *
    47.   */
    48. var $pagebarnum=10;//控制记录条的个数。
    49. var $totalpage=0;//总页数
    50. var $ajax_action_name='';//AJAX动作名
    51. var $nowindex=1;//当前页
    52. var $url="";//url地址头
    53. var $offset=0;

    54. /**
    55.   * constructor构造函数
    56.   *
    57.   * @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']...
    58.   */
    59. function page($array)
    60. {
    61.   if(is_array($array)){
    62.      if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param of total');
    63.      $total=intval($array['total']);
    64.      $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
    65.      $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
    66.      $url=(array_key_exists('url',$array))?$array['url']:'';
    67.   }else{
    68.      $total=$array;
    69.      $perpage=10;
    70.      $nowindex='';
    71.      $url='';
    72.   }
    73.   if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a positive integer!');
    74.   if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
    75.   if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);//设置pagename
    76.   $this->_set_nowindex($nowindex);//设置当前页
    77.   $this->_set_url($url);//设置链接地址
    78.   $this->totalpage=ceil($total/$perpage);
    79.   $this->offset=($this->nowindex-1)*$perpage;
    80.   if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式
    81. }
    82. /**
    83.   * 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception
    84.   *
    85.   * @param string $var
    86.   * @param string $value
    87.   */
    88. function set($var,$value)
    89. {
    90.   if(in_array($var,get_object_vars($this)))
    91.      $this->$var=$value;
    92.   else {
    93.    $this->error(__FUNCTION__,$var." does not belong to PB_Page!");
    94.   }
    95.   
    96. }
    97. /**
    98.   * 打开倒AJAX模式
    99.   *
    100.   * @param string $action 默认ajax触发的动作。
    101.   */
    102. function open_ajax($action)
    103. {
    104.   $this->is_ajax=true;
    105.   $this->ajax_action_name=$action;
    106. }
    107. /**
    108.   * 获取显示"下一页"的代码
    109.   *
    110.   * @param string $style
    111.   * @return string
    112.   */
    113. function next_page($style='')
    114. {
    115.   if($this->nowindex<$this->totalpage){
    116.    return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
    117.   }
    118.   return '<span class="'.$style.'">'.$this->next_page.'</span>';
    119. }

    120. /**
    121.   * 获取显示“上一页”的代码
    122.   *
    123.   * @param string $style
    124.   * @return string
    125.   */
    126. function pre_page($style='')
    127. {
    128.   if($this->nowindex>1){
    129.    return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
    130.   }
    131.   return '<span class="'.$style.'">'.$this->pre_page.'</span>';
    132. }

    133. /**
    134.   * 获取显示“首页”的代码
    135.   *
    136.   * @return string
    137.   */
    138. function first_page($style='')
    139. {
    140.   if($this->nowindex==1){
    141.       return '<span class="'.$style.'">'.$this->first_page.'</span>';
    142.   }
    143.   return $this->_get_link($this->_get_url(1),$this->first_page,$style);
    144. }

    145. /**
    146.   * 获取显示“尾页”的代码
    147.   *
    148.   * @return string
    149.   */
    150. function last_page($style='')
    151. {
    152.   if($this->nowindex==$this->totalpage){
    153.       return '<span class="'.$style.'">'.$this->last_page.'</span>';
    154.   }
    155.   return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
    156. }

    157. function nowbar($style='',$nowindex_style='cururl')
    158. {
    159.   $plus=ceil($this->pagebarnum/2);
    160.   if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
    161.   $begin=$this->nowindex-$plus+1;
    162.   $begin=($begin>=1)?$begin:1;
    163.   $return='';
    164.   for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
    165.   {
    166.    if($i<=$this->totalpage){
    167.     if($i!=$this->nowindex)
    168.         $return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
    169.     else
    170.         $return.=$this->_get_text('<span class="'.$nowindex_style.'">'.$i.'</span>');
    171.    }else{
    172.     break;
    173.    }
    174.    $return.="\n";
    175.   }
    176.   unset($begin);
    177.   return $return;
    178. }
    179. /**
    180.   * 获取显示跳转按钮的代码
    181.   *
    182.   * @return string
    183.   */
    184. function select()
    185. {
    186.    $return='<select name="'.$this->selectname.'">';
    187.   for($i=1;$i<=$this->totalpage;$i++)
    188.   {
    189.    if($i==$this->nowindex){
    190.     $return.='<option value="'.$i.'" selected>'.$i.'</option>';
    191.    }else{
    192.     $return.='<option value="'.$i.'">'.$i.'</option>';
    193.    }
    194.   }
    195.   unset($i);
    196.   $return.='</select>';
    197.   return $return;
    198. }

    199. /**
    200.   * 获取mysql 语句中limit需要的值
    201.   *
    202.   * @return string
    203.   */
    204. function offset()
    205. {
    206.   return $this->offset;
    207. }

    208. /**
    209.   * 控制分页显示风格(你可以增加相应的风格)
    210.   *
    211.   * @param int $mode
    212.   * @return string
    213.   */
    214. function show($mode=1)
    215. {
    216.   switch ($mode)
    217.   {
    218.    case '1':
    219.     $this->next_page='下一页';
    220.     $this->pre_page='上一页';
    221.     return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
    222.     break;
    223.    case '2':
    224.     $this->next_page='下一页';
    225.     $this->pre_page='上一页';
    226.     $this->first_page='首页';
    227.     $this->last_page='尾页';
    228.     return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
    229.     break;
    230.    case '3':
    231.     $this->next_page='下一页';
    232.     $this->pre_page='上一页';
    233.     $this->first_page='首页';
    234.     $this->last_page='尾页';
    235.     return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
    236.     break;
    237.    case '4':
    238.     $this->next_page='下一页';
    239.     $this->pre_page='上一页';
    240.     return $this->pre_page().$this->nowbar().$this->next_page();
    241.     break;
    242.    case '5':
    243.     return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
    244.     break;
    245.   }
    246.   
    247. }
    248. /*----------------private function (私有方法)-----------------------------------------------------------*/
    249. /**
    250.   * 设置url头地址
    251.   * @param: String $url
    252.   * @return boolean
    253.   */
    254. function _set_url($url="")
    255. {
    256.   if(!empty($url)){
    257.       //手动设置
    258.    $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
    259.   }else{
    260.       //自动获取
    261.    if(empty($_SERVER['QUERY_STRING'])){
    262.        //不存在QUERY_STRING时
    263.     $this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
    264.    }else{
    265.        //
    266.     if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
    267.         //地址存在页面参数
    268.      $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);
    269.      $last=$this->url[strlen($this->url)-1];
    270.      if($last=='?'||$last=='&'){
    271.          $this->url.=$this->page_name."=";
    272.      }else{
    273.          $this->url.='&'.$this->page_name."=";
    274.      }
    275.     }else{
    276.         //
    277.      $this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
    278.     }//end if   
    279.    }//end if
    280.   }//end if
    281. }

    282. /**
    283.   * 设置当前页面
    284.   *
    285.   */
    286. function _set_nowindex($nowindex)
    287. {
    288.   if(empty($nowindex)){
    289.    //系统获取
    290.    
    291.    if(isset($_GET[$this->page_name])){
    292.     $this->nowindex=intval($_GET[$this->page_name]);
    293.    }
    294.   }else{
    295.       //手动设置
    296.    $this->nowindex=intval($nowindex);
    297.   }
    298. }
    299.   
    300. /**
    301.   * 为指定的页面返回地址值
    302.   *
    303.   * @param int $pageno
    304.   * @return string $url
    305.   */
    306. function _get_url($pageno=1)
    307. {
    308.   return $this->url.$pageno;
    309. }


    310. /**
    311.   * 获取分页显示文字,比如说默认情况下_get_text('<a href="">1</a>')将返回[<a href="">1</a>]
    312.   *
    313.   * @param String $str
    314.   * @return string $url
    315.   */
    316. function _get_text($str)
    317. {
    318.   return $this->format_left.$str.$this->format_right;
    319. }

    320. /**
    321.    * 获取链接地址
    322. */
    323. function _get_link($url,$text,$style=''){
    324.   $style=(empty($style))?'':'class="'.$style.'"';
    325.   if($this->is_ajax){
    326.       //如果是使用AJAX模式
    327.    return '<a '.$style.' href="javascript:'.$this->ajax_action_name.'(\''.$url.'\')">'.$text.'</a>';
    328.   }else{
    329.    return '<a '.$style.' href="'.$url.'">'.$text.'</a>';
    330.   }
    331. }
    332. /**
    333.    * 出错处理方式
    334. */
    335. function error($function,$errormsg)
    336. {
    337.      die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.$function.'()</b> :'.$errormsg);
    338. }
    339. }
    340. ?>
    复制代码
    [img=http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=fRUcHhYWGAQ9GxIFEBwUEVMeEhA]http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png[/img]

    使用道具 举报

    Rank: 8Rank: 8

  • TA的每日心情
    无聊
    2011-10-16 14:36:38
  • 签到天数: 21 天

    [LV.4]偶尔看看III

    论坛先锋 活跃之星 学习至圣 论坛元老 荣誉成员

    板凳
    发表于 2011-8-27 06:52:24 |只看该作者
    我给你支持下..

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册 人人连接登陆

    晴云孤魂's Blog|就爱编程搜帖|手机版|Archiver|就爱编程论坛     

    GMT+8, 2025-7-2 13:32 , Processed in 0.095102 second(s), 30 queries .

    Powered by Discuz! X2

    © 2001-2011 Comsenz Inc.

    回顶部