1 <?php 2 class SubPages { 3 4 private $each_disNums; //每页显示的条目数 5 private $nums; //总条目数 6 private $current_page; //当前被选中的页 7 private $sub_pages; //每次显示的页数 8 private $pageNums; //总页数 9 private $page_array = array(); //用来构造分页的数组 10 private $subPage_link; //每个分页的链接 11 private $subPage_type; //显示分页的类型 12 13 /* 14 construct是SubPages的构造函数,用来在创建类的时候自动运行. 15 @$each_disNums 每页显示的条目数 自定义一个变量赋值为页数:$page = 2; 16 @nums 总条目数 (统计表中要查找的记录数) 17 @current_num 当前被选中的页(在subpage_link这个参数中传输一个值:Messageshow.php?cur_page=,在接收页面接收) 18 @sub_pages 每次显示的页数(每次显示的页数=总条目数/每页显示的条目数) 19 @subPage_link 每个分页的链接(显示这些记录的页面) 20 @subPage_type 显示分页的类型 21 当@subPage_type=1的时候为普通分页模式 22 example: 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 23 当@subPage_type=2的时候为经典分页样式 24 example: 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页] 25 */ 26 27 function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_link, $subPage_type) { 28 $this->each_disNums = intval($each_disNums); 29 $this->nums = intval($nums); 30 if (!$current_page) { 31 $this->current_page = 1; 32 } else { 33 $this->current_page = intval($current_page); 34 } 35 $this->sub_pages = intval($sub_pages); 36 $this->pageNums = ceil($nums / $each_disNums); 37 $this->subPage_link = $subPage_link; 38 $this->show_SubPages($subPage_type); 39 //echo $this->pageNums."--".$this->sub_pages; 40 } 41 42 /* 43 __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。 44 */ 45 46 function __destruct() { 47 //释放变量 48 unset($each_disNums); 49 unset($nums); 50 unset($current_page); 51 unset($sub_pages); 52 unset($pageNums); 53 unset($page_array); 54 unset($subPage_link); 55 unset($subPage_type); 56 } 57 58 /* 59 show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页 60 */ 61 62 function show_SubPages($subPage_type) { 63 if ($subPage_type == 1) { 64 $this->subPageCss1(); 65 } elseif ($subPage_type == 2) { 66 $this->subPageCss2(); 67 } 68 } 69 70 /* 用来给建立分页的数组初始化的函数。 71 */ 72 73 function initArray() { 74 for ($i = 0; $i < $this->sub_pages; $i++) { 75 $this->page_array[$i] = $i; 76 } return $this->page_array; 77 } 78 79 /* construct_num_Page该函数使用来构造显示的条目 80 * 即使:[1][2][3][4][5][6][7][8][9][10] 81 */ 82 83 function construct_num_Page() { 84 if ($this->pageNums < $this->sub_pages) { 85 $current_array = array(); 86 for ($i = 0; $i < $this->pageNums; $i++) { 87 $current_array[$i] = $i + 1; 88 } 89 } else { 90 $current_array = $this->initArray(); 91 if ($this->current_page <= 3) { 92 for ($i = 0; $i < count($current_array); $i++) { 93 $current_array[$i] = $i + 1; 94 } 95 } elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) { 96 for ($i = 0; $i < count($current_array); $i++) { 97 $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i; 98 } 99 } else { 100 for ($i = 0; $i < count($current_array); $i++) { 101 $current_array[$i] = $this->current_page - 2 + $i; 102 } 103 } 104 } return $current_array; 105 } 106 107 /* 构造普通模式的分页 108 * 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 109 * 110 */ 111 112 function subPageCss1() { 113 $subPageCss1Str = ""; 114 $subPageCss1Str.="共" . $this->nums . "条记录,"; 115 $subPageCss1Str.="每页显示" . $this->each_disNums . "条,"; 116 $subPageCss1Str.="当前第" . $this->current_page . "/" . $this->pageNums . "页 "; 117 if ($this->current_page > 1) { 118 $firstPageUrl = $this->subPage_link . "1"; 119 $prewPageUrl = $this->subPage_link . ($this->current_page - 1); 120 $subPageCss1Str.="[<a href='$firstPageUrl'>首页</a>] "; 121 $subPageCss1Str.="[<a href='$prewPageUrl'>上一页</a>] "; 122 } else { 123 $subPageCss1Str.="[首页] "; 124 $subPageCss1Str.="[上一页] "; 125 } if ($this->current_page < $this->pageNums) { 126 $lastPageUrl = $this->subPage_link . $this->pageNums; 127 $nextPageUrl = $this->subPage_link . ($this->current_page + 1); 128 $subPageCss1Str.=" [<a href='$nextPageUrl'>下一页</a>] "; 129 $subPageCss1Str.="[<a href='$lastPageUrl'>尾页</a>] "; 130 } else { 131 $subPageCss1Str.="[下一页] "; 132 $subPageCss1Str.="[尾页] "; 133 } echo $subPageCss1Str; 134 } 135 136 /* 137 * 构造经典模式的分页 138 * 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页] 139 */ 140 141 function subPageCss2() { 142 $subPageCss2Str = "共有".$this->nums."条记录 "; 143 $subPageCss2Str.="当前第" . $this->current_page . "/" . $this->pageNums . "页 "; 144 if ($this->current_page > 1) { 145 $firstPageUrl = $this->subPage_link . "1"; 146 $prewPageUrl = $this->subPage_link . ($this->current_page - 1); 147 $subPageCss2Str.="[<a href='$firstPageUrl'>首页</a>] "; 148 $subPageCss2Str.="[<a href='$prewPageUrl'>上一页</a>] "; 149 } else { 150 $subPageCss2Str.="[首页] "; 151 $subPageCss2Str.="[上一页] "; 152 } $a = $this->construct_num_Page(); 153 for ($i = 0; $i < count($a); $i++) { 154 $s = $a[$i]; 155 if ($s == $this->current_page) { 156 $subPageCss2Str.="[<span style='color:red;font-weight:bold;'>" . $s . "</span>]"; 157 } else { 158 $url = $this->subPage_link . $s; 159 $subPageCss2Str.="[<a href='$url'>" . $s . "</a>]"; 160 } 161 } if ($this->current_page < $this->pageNums) { 162 $lastPageUrl = $this->subPage_link . $this->pageNums; 163 $nextPageUrl = $this->subPage_link . ($this->current_page + 1); 164 $subPageCss2Str.=" [<a href='$nextPageUrl'>下一页</a>] "; 165 $subPageCss2Str.="[<a href='$lastPageUrl'>尾页</a>] "; 166 } else { 167 $subPageCss2Str.="[下一页] "; 168 $subPageCss2Str.="[尾页] "; 169 } echo $subPageCss2Str; 170 } 171 172 } 173 ?>