1 <?php 2 3 /** 4 * 页面名称:cls_page.php 5 */ 6 class Page { 7 private $each_disNums; //每页显示的条目数 8 private $nums; //总条目数 9 private $current_page; //当前被选中的页 10 private $sub_pages; //每次显示的页数 11 private $pageNums; //总页数 12 private $page_array = array (); //用来构造分页的数组 13 private $subPage_link; //每个分页的链接 14 15 16 /** 17 * 18 * __construct是SubPages的构造函数,用来在创建类的时候自动运行. 19 * @$each_disNums 每页显示的条目数 20 * @nums 总条目数 21 * @current_num 当前被选中的页 22 * @sub_pages 每次显示的页数 23 * @subPage_link 每个分页的链接 24 * @subPage_type 显示分页的类型 25 * 26 * 当@subPage_type=1的时候为普通分页模式 27 * example: 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 28 * 当@subPage_type=2的时候为经典分页样式 29 * example: 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页] 30 */ 31 function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_link) { 32 $this->each_disNums = intval($each_disNums); 33 $this->nums = intval($nums); 34 if (!$current_page) { 35 $this->current_page = 1; 36 } else { 37 $this->current_page = intval($current_page); 38 } 39 $this->sub_pages = intval($sub_pages); 40 $this->pageNums = ceil($nums / $each_disNums); 41 $this->subPage_link = $subPage_link; 42 } 43 /** 44 * 照顾低版本 45 */ 46 /* 47 function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_linke) { 48 $this->Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link); 49 50 } 51 */ 52 53 /* 54 __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。 55 */ 56 function __destruct() { 57 unset ($each_disNums); 58 unset ($nums); 59 unset ($current_page); 60 unset ($sub_pages); 61 unset ($pageNums); 62 unset ($page_array); 63 unset ($subPage_link); 64 } 65 66 /* 67 用来给建立分页的数组初始化的函数。 68 */ 69 function initArray() { 70 for ($i = 0; $i < $this->sub_pages; $i++) { 71 $this->page_array[$i] = $i; 72 } 73 return $this->page_array; 74 } 75 76 /* 77 construct_num_Page该函数使用来构造显示的条目 78 即使:[1][2][3][4][5][6][7][8][9][10] 79 */ 80 function construct_num_Page() { 81 if ($this->pageNums < $this->sub_pages) { 82 $current_array = array (); 83 for ($i = 0; $i < $this->pageNums; $i++) { 84 $current_array[$i] = $i +1; 85 } 86 } else { 87 $current_array = $this->initArray(); 88 if ($this->current_page <= 3) { 89 for ($i = 0; $i < count($current_array); $i++) { 90 $current_array[$i] = $i +1; 91 } 92 } 93 elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) { 94 for ($i = 0; $i < count($current_array); $i++) { 95 $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i; 96 } 97 } else { 98 for ($i = 0; $i < count($current_array); $i++) { 99 $current_array[$i] = $this->current_page - 2 + $i; 100 } 101 } 102 } 103 104 return $current_array; 105 } 106 107 /* 108 构造普通模式的分页 109 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 110 */ 111 function subPageCss1() { 112 $subPageCss1Str = ""; 113 $subPageCss1Str .= "共" . $this->nums . "条记录,"; 114 $subPageCss1Str .= "每页显示" . $this->each_disNums . "条,"; 115 $subPageCss1Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 "; 116 if ($this->current_page > 1) { 117 $firstPageUrl = $this->subPage_link . "1"; 118 $prewPageUrl = $this->subPage_link . ($this->current_page - 1); 119 $subPageCss1Str .= "[<a href='$firstPageUrl'>首页</a>] "; 120 $subPageCss1Str .= "[<a href='$prewPageUrl'>上一页</a>] "; 121 } else { 122 $subPageCss1Str .= "[首页] "; 123 $subPageCss1Str .= "[上一页] "; 124 } 125 126 if ($this->current_page < $this->pageNums) { 127 $lastPageUrl = $this->subPage_link . $this->pageNums; 128 $nextPageUrl = $this->subPage_link . ($this->current_page + 1); 129 $subPageCss1Str .= " [<a href='$nextPageUrl'>下一页</a>] "; 130 $subPageCss1Str .= "[<a href='$lastPageUrl'>尾页</a>] "; 131 } else { 132 $subPageCss1Str .= "[下一页] "; 133 $subPageCss1Str .= "[尾页] "; 134 } 135 136 return $subPageCss1Str; 137 138 } 139 140 /* 141 构造经典模式的分页 142 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页] 143 */ 144 function subPageCss2() { 145 $subPageCss2Str = ""; 146 $subPageCss2Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 "; 147 148 if ($this->current_page > 1) { 149 $firstPageUrl = $this->subPage_link . "1"; 150 $prewPageUrl = $this->subPage_link . ($this->current_page - 1); 151 $subPageCss2Str .= "[<a href='$firstPageUrl'>首页</a>] "; 152 $subPageCss2Str .= "[<a href='$prewPageUrl'>上一页</a>] "; 153 } else { 154 $subPageCss2Str .= "[首页] "; 155 $subPageCss2Str .= "[上一页] "; 156 } 157 158 $a = $this->construct_num_Page(); 159 for ($i = 0; $i < count($a); $i++) { 160 $s = $a[$i]; 161 if ($s == $this->current_page) { 162 $subPageCss2Str .= "[<span style='color:red;font-weight:bold;'>" . $s . "</span>]"; 163 } else { 164 $url = $this->subPage_link . $s; 165 $subPageCss2Str .= "[<a href='$url'>" . $s . "</a>]"; 166 } 167 } 168 169 if ($this->current_page < $this->pageNums) { 170 $lastPageUrl = $this->subPage_link . $this->pageNums; 171 $nextPageUrl = $this->subPage_link . ($this->current_page + 1); 172 $subPageCss2Str .= " [<a href='$nextPageUrl'>下一页</a>] "; 173 $subPageCss2Str .= "[<a href='$lastPageUrl'>尾页</a>] "; 174 } else { 175 $subPageCss2Str .= "[下一页] "; 176 $subPageCss2Str .= "[尾页] "; 177 } 178 return $subPageCss2Str; 179 } 180 } 181 182 183 ?> 184 185 186 187 <? 188 class sqldao{ 189 //连接数据库 190 function conn(){ 191 $dbh=mysql_connect('localhost','username','pass')or die("对不起,数据库连接错误!请稍候再来,或与管理员联系"); 192 mysql_query("set names 'utf8'", $dbh); 193 mysql_select_db('tableName'); 194 return $dbh; 195 } 196 } 197 ?> 198 199 200 201 <? 202 //以下为测试代码 203 $sqldao=new sqldao(); 204 $dbh=$sqldao->conn(); 205 206 $start=($_GET['p']-1)*10; 207 $que="select utname from renyuan"; 208 $rs=mysql_query($que,$dbh); 209 $num_all=mysql_num_rows($rs); 210 211 $rs=mysql_query($que." limit ".$start.",10",$dbh); 212 $num=mysql_num_rows($rs); 213 for($i=0;$i<$num;$i++){ 214 $row=mysql_fetch_array($rs); 215 echo $row['utname']."<br>"; 216 } 217 218 219 echo "<hr>"; 220 //测试一下,看看两种不同效果 221 //$t = new Page(显示条数, 数据总数, 页数, 页数链接个数, '页面链接'); 222 $t = new Page(10, $num_all, $_GET['p'], 10, 'cls_page.php?p='); 223 echo $t->subPageCss2(); 224 echo "<p>"; 225 echo $t->subPageCss1(); 226 227 ?>