分页类讲解

PHP分页原理详解:

分页原理详解,其实各个语言的都差不多,主要是程序跟数据库的表达方式不一样。

在看本文之前,请确保你已掌握了PHP的一些知识以及MYSQL的查询操作基础哦。

作为一个Web程序,经常要和不计其数的数据打交道,比如会员的数据,文章数据,假如只有几十个会员那很好办,在一页显示就可以了,可是假如你的网站是几千甚至几十万会员的话,如果都在一页打开的话无论对浏览器还是观看者都是一种折磨,而且如果数据上亿,从数据库里查询一次的话,对服务器的压力是很大的,这不是正确的方法。

相信每个学习PHP的新手都会对分页这个东西感觉很头疼,不过有了默默的这一水帖,你肯定会拍拍脑袋说,嘿,原来分页竟然如此简单?的确,现在请深呼吸一口新鲜的空气,仔细的听默默给你一点一点的分解。

假设我们要处理1000条数据,要在每页中显示10条,这样的话就会分100页来显示,咱们先看一看在mysql里提取10条信息是如何操作的。

Select * from table limit 0,10

上面是一句很简单的mysql查询语句,它的作用是从一个名叫table的表里提取10条数据,并且把所有字段的值都获得。其中的limit 0,10的用法是:limit 开始点,要提取的数目

关键的地方就在这段“limit 0,10”,它其中的0是以0为起始点,后面的10则是显示10条数据,那么我们要以10为起始点,显示到第20条数据该怎么写呢?

可能很多大大会心直口快的说“limit 10,20”嘛!啊哦,这样可就错误了哦,正确的写法是“limit 10,10”它后面的参数并非是结束点而是要提取的数目 ,记住哦。

懂得了如何提取10条数据,那么提取1000条也就是做100次这种查询呀,就是说要做如下的查询:

Limit 0,10                 //第一页
Limit 10,10                //第二页
Limit 20,10                //第三页
Limit 30,10                //第四页
……
看出有什么规律了吗?没错,第一个参数每翻一页就增加10,可是第二个参数是不变的。
也就是说咱们设法根据页数来改变第一个参数的值 ,就可以进行分页显示数据了,怎么样,原理是不是很简单?

可是要怎么设法根据页数来改变第一个参数的值呢?首先,咱们要有一个页数的值,用url的GET方式获取。
比如index.php?page=18
相信大部分的大大对这个东西不陌生吧,这种url地址可是随处可见,其中的page参数的作用就是传入要显示的页数。

咱们通过一段代码来看一看究竟是如何实现的吧:

[php]
<?php

/*

Author:默默
Date :2006-12-03

*/

$page=isset($_GET['page'])?intval($_GET['page']):1;        //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1。
$num=10;         //每页显示10条数据

$db=mysql_connect("host","name","pass");           //创建数据库连接
$select=mysql_select_db("db",$db);                 //选择要操作的数据库

/*
首先咱们要获取数据库中到底有多少数据,才能判断具体要分多少页,总页数 具体的公式就是
总数据数 除以 每页显示的条数,有余进一
也就是说10/3=3.3333=4 有余数就要进一。
*/

$total=mysql_num_rows(mysql_query("select * from table")); //查询数据的总数total
$pagenum=ceil($total/$num);      //获得总页数 pagenum

//假如传入的页数参数apge 大于总页数 pagenum,则显示错误信息
If($page>$pagenum || $page == 0){
       Echo "Error : Can Not Found The page .";
       Exit;
}

$offset=($page-1)*$num;         //获取limit的第一个参数的值 offset ,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。             (传入的页数-1) * 每页的数据 得到limit第一个参数的值

$info=mysql_query("select * from table limit $offset,$num ");   //获取相应页数所需要显示的数据
While($it=mysql_fetch_array($info)){
       Echo $it['name']."<br />";
}                                                              //显示数据

For($i=1;$i<=$pagenum;$i++){

       $show=($i!=$page)?"<a href='index.php?page=".$i."'>$i</a>":"<b>$i</b>";
       Echo $show." ";
}

/*显示分页信息,假如是当页则显示粗体的数字,其余的页数则为超连接,假如当前为第三页则显示如下
1 2 3 4 5 6
*/
?>
   [/php]
假如你仔细的读过上面的代码,把数据库连接和查询的表替换成你的,那么就能看见它的执行效果哦。

是不是很简单,只要动动脑筋,可以让它显示的更为个性化哦,给大家出一个小题,如何实现“首页 上一页 下一页 尾页”这种格式的分页呢?

OK,水帖灌完,收工。^_^ 默默小谈PHP&MYSQL分页原理及实现
===================================
总结:

原型:      Select * from table limit 0,10
程序:
select * from table limit $offset,$num ($offset取值是:传入的页面数-1     $num是每个页面显示的数据,多为固定常量值)

总分页数:总数据 每页显示的条数有余进一
    int totalPage=((totalCount%NUM)==0)?totalCount/NUM:totalCount/NUM+1;

limit用法limit 开始点,要提取的数目

不过要注意的是:一定要加上order by ,确定以上升或者下降的顺序来查询,不然在查询的时候会不知道从哪个方向开始查询。不过一定要注意顺序:正确的是select * from user order by id desc limit 0,10;

 简单的完整分页类实例一:

 1 <?php
 2 //conn.php
 3 $host ="127.0.0.1";
 4 $dbUser = 'root';               // db User
 5 $dbPass = '';             // db User Password
 6 
 7 $con = mysql_connect($host,$dbUser,$dbPass);
 8 if (!$con)
 9   {
10   die('Could not connect: ' . mysql_error());
11   }
12 
13 mysql_select_db("world", $con);
14 ?>
View Code
 1 <?php
 2 //page.php
 3 include("conn.php"); //数据库链接
 4  
 5 $sql="select * from city"; //city数据表
 6 
 7  $query=mysql_query($sql);
 8 
 9  $all_num=mysql_num_rows($query); //总条数
10 
11 $page_num=30; //每页条数
12 
13  $page_all_num = ceil($all_num/$page_num); //总页数 
14 
15  $page=empty($_GET['page'])?1:$_GET['page']; //当前页数
16 
17  $page=(int)$page; //安全强制转换 
18 
19  $limit_st = ($page-1)*$page_num; //起始数
20 
21 //=================================================
22 
23  $sql="select * from city limit $limit_st , $page_num";
24  
25  $query=mysql_query($sql);
26 
27  while($row=mysql_fetch_array($query)){
28 
29        echo $row['Name']." <a href='view.php?id={$row['ID']}'>浏览</a><hr>";
30 
31 } 
32 
33  $px = $page>=$page_all_num ? $page_all_num : $page+1 ; 
34 
35  $ps = $page<=1 ? 1 : $page-1 ; 
36 
37 ?>
38 
39 <HTML>
40 <HEAD></HEAD>
41 <BODY>
42 <a href='page.php'>首页 </a> | 
43 
44 <a href='page.php?page=<?php echo $ps?>'>上一页 </a> | 
45 
46 <a href='page.php?page=<?php echo $px?>'>下一页 </a> | 
47 
48 <a href='page.php?page=<?php echo $page_all_num?>'>尾页 </a>
49 
50 </BODY>
51 </HTML>
View Code
 1 <?php
 2 //view.php显示页
 3 include "conn.php";
 4 $id=$_GET['id'];
 5 $result = mysql_query("SELECT * FROM city where ID=$id");
 6 echo "<table border='1'>
 7 <tr>
 8 <th>ID</th>
 9 <th>Name</th>
10 <th>CountryCode</th>
11 <th>District</th>
12 <th>Population</th>
13 </tr>";
14 
15 while($row = mysql_fetch_array($result))
16   {
17   echo "<tr>";
18   echo "<td>" . $row['ID'] . "</td>";
19   echo "<td>" . $row['Name'] . "</td>";
20    echo "<td>" . $row['CountryCode'] . "</td>";
21     echo "<td>" . $row['District'] . "</td>";
22      echo "<td>" . $row['Population'] . "</td>";
23   echo "</tr>";
24   }
25 echo "</table>";
26 
27 ?>
View Code

 简单的完整分页类实例二:

 1 <?php
 2 //conn.php
 3 $host ="127.0.0.1";
 4 $dbUser = 'root';               // db User
 5 $dbPass = '';             // db User Password
 6 
 7 $con = mysql_connect($host,$dbUser,$dbPass);
 8 if (!$con)
 9   {
10   die('Could not connect: ' . mysql_error());
11   }
12 
13 mysql_select_db("world", $con);
14 
15 ?>
View Code
  1 <?php  
  2 
  3  //分页类  page.php
  4 class Core_Lib_Page  
  5 
  6 {  
  7 
  8  public     $first_row;        //起始行数  
  9  public     $list_rows;        //列表每页显示行数  
 10 
 11 protected  $total_pages;      //总页数  
 12 
 13 protected  $total_rows;       //总行数  
 14 
 15 protected  $now_page;         //当前页数  
 16 
 17 protected  $method  = 'defalut'; //处理情况 Ajax分页 Html分页(静态化时) 普通get方式   
 18 
 19  protected  $parameter = '';  
 20 
 21 protected  $page_name;        //分页参数的名称  
 22 protected  $ajax_func_name;  
 23 
 24 public     $plus = 3;         //分页偏移量  
 25  protected  $url;  
 26 
 27 /**  
 28 
 29  * 构造函数  
 30 
 31  * @param unknown_type $data  
 32 
 33  */ 
 34 
 35 public function __construct($data = array())  
 36 
 37 {  
 38 
 39  $this->total_rows = $data['total_rows'];  
 40 
 41 $this->parameter         = !empty($data['parameter']) ? $data['parameter'] : '';  
 42 
 43 $this->list_rows         = !empty($data['list_rows']) && $data['list_rows'] <= 3000 ? $data['list_rows'] : 3000;  
 44 
 45  $this->total_pages       = ceil($this->total_rows / $this->list_rows);  
 46 
 47  $this->page_name         = !empty($data['page_name']) ? $data['page_name'] : 'p';  
 48 
 49 $this->ajax_func_name    = !empty($data['ajax_func_name']) ? $data['ajax_func_name'] : '';  
 50 
 51  $this->method           = !empty($data['method']) ? $data['method'] : '';  
 52 
 53 /* 当前页面 */ 
 54 
 55  if(!empty($data['now_page']))  
 56 
 57 {  
 58 
 59  $this->now_page = intval($data['now_page']);  
 60 
 61 }else{  
 62 
 63  $this->now_page   = !empty($_GET[$this->page_name]) ? intval($_GET[$this->page_name]):1;  
 64 
 65   }  
 66 
 67 $this->now_page   = $this->now_page <= 0 ? 1 : $this->now_page;  
 68 
 69 if(!empty($this->total_pages) && $this->now_page > $this->total_pages)  
 70 
 71  {  
 72 
 73    $this->now_page = $this->total_pages;  
 74 
 75     }  
 76 
 77   $this->first_row = $this->list_rows * ($this->now_page - 1);  
 78 
 79 }     
 80 
 81 /**
 82      * 得到当前连接
 83      * @param $page
 84      * @param $text
 85      * @return string
 86      */
 87  protected function _get_link($page,$text)  
 88 
 89 {  
 90 
 91  switch ($this->method) {  
 92 
 93  case 'ajax':  
 94 
 95 $parameter = '';  
 96 
 97 if($this->parameter)  
 98 
 99  {  
100 
101    $parameter = ','.$this->parameter;  
102 
103  }  
104 
105  return '<a onclick="' . $this->ajax_func_name . '(\'' . $page . '\''.$parameter.')" href="javascript:void(0)">' . $text . '</a>' . "\n";  
106 
107  break;  
108 
109  case 'html':  
110 
111  $url = str_replace('?', $page,$this->parameter);  
112 
113 return '<a href="' .$url . '">' . $text . '</a>' . "\n";  
114 
115 break;  
116  default:  
117 
118 return '<a href="' . $this->_get_url($page) . '">' . $text . '</a>' . "\n";  
119  break;  
120 
121  }  
122 
123 }  
124 
125 /**
126      * 设置当前页面链接
127      */
128 protected function _set_url()  
129 
130 {  
131 
132   $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;  
133 
134   $parse = parse_url($url);  
135 
136  if(isset($parse['query'])) {  
137 
138  parse_str($parse['query'],$params);  
139 
140  unset($params[$this->page_name]);  
141  $url   =  $parse['path'].'?'.http_build_query($params);  
142 
143 }  
144 
145  if(!empty($params))  
146 
147  {  
148 
149  $url .= '&';  
150 
151   }  
152 
153 $this->url = $url;  
154 
155  }  
156 
157 /**
158      * 得到$page的url
159      * @param $page 页面
160      * @return string
161      */
162 
163 protected function _get_url($page)  
164 
165 {  
166 
167 if($this->url === NULL)  
168 
169  {  
170 
171  $this->_set_url();     
172 
173  }  
174 
175  //  $lable = strpos('&', $this->url) === FALSE ? '' : '&';  
176 
177  return $this->url . $this->page_name . '=' . $page;  
178 
179  }  
180 
181 /**
182      * 得到第一页
183      * @return string
184      */
185 
186 public function first_page($name = '第一页')  
187 
188 {  
189 
190  if($this->now_page > 5)  
191 
192  {  
193 
194  return $this->_get_link('1', $name);  
195 
196  }     
197 
198 return '';  
199 
200 }  
201 
202  
203  /**
204      * 最后一页
205      * @param $name
206      * @return string
207      */
208 
209  public function last_page($name = '最后一页')  
210 
211 {  
212 
213 if($this->now_page < $this->total_pages - 5)  
214 
215 {  
216 
217   return $this->_get_link($this->total_pages, $name);  
218 
219 }     
220 
221  return '';  
222 
223 }    
224 
225   /**
226      * 上一页
227      * @return string
228      */
229 
230 public function up_page($name = '上一页')  
231 
232  {  
233 
234  if($this->now_page != 1)  
235 
236 {  
237 
238  return $this->_get_link($this->now_page - 1, $name);  
239 
240  }  
241 
242  return '';  
243 
244 }  
245 
246  /**
247      * 下一页
248      * @return string
249      */
250 
251 public function down_page($name = '下一页')  
252 
253  {  
254 
255 if($this->now_page < $this->total_pages)  
256 
257  {  
258 
259 return $this->_get_link($this->now_page + 1, $name);  
260 
261   }  
262  return '';  
263 
264  }  
265 
266   /**
267      * 分页样式输出
268      * @param $param
269      * @return string
270      */
271 
272  public function show($param = 1)  
273 
274 {  
275 
276  if($this->total_rows < 1)  
277 
278 {  
279 
280  return '';  
281 
282  }  
283 
284  $className = 'show_' . $param;  
285 
286  $classNames = get_class_methods($this);  
287 if(in_array($className, $classNames))  
288 
289  {  
290 
291   return $this->$className();  
292 
293  }  
294 
295 return '';  
296 
297 }  
298 
299  protected function show_2()  
300 
301  {  
302 
303  if($this->total_pages != 1)  
304 
305  {  
306 
307  $return = '';  
308 
309  $return .= $this->up_page('<');  
310 
311  for($i = 1;$i<=$this->total_pages;$i++)  
312 
313 {  
314 
315  if($i == $this->now_page)  
316 
317  {  
318 
319  $return .= "<a class='now_page'>$i</a>\n";  
320 
321 }  
322 
323  else 
324 
325  {  
326 
327  if($this->now_page-$i>=4 && $i != 1)  
328 
329  {  
330  $return .="<span class='pageMore'>...</span>\n";  
331 
332 $i = $this->now_page-3;  
333 
334 }  
335 
336  else 
337 
338  {  
339 
340  if($i >= $this->now_page+5 && $i != $this->total_pages)  
341 
342 {  
343 
344 $return .="<span>...</span>\n";   
345 
346  $i = $this->total_pages;  
347 
348 }  
349 $return .= $this->_get_link($i, $i) . "\n";  
350 
351  }  
352 
353  }  
354 
355  }  
356 
357  $return .= $this->down_page('>');  
358 
359  return $return;  
360 
361  }  
362 
363 }  
364 
365  protected function show_1()  
366 
367 {  
368 
369  $plus = $this->plus;  
370 
371 if( $plus + $this->now_page > $this->total_pages)  
372 
373 {  
374 
375  $begin = $this->total_pages - $plus * 2;  
376 
377  }else{  
378 
379  $begin = $this->now_page - $plus;  
380 
381  }  
382 
383   $begin = ($begin >= 1) ? $begin : 1;  
384 
385   $return = '';  
386 
387 $return .= $this->first_page();  
388 
389 $return .= $this->up_page();  
390 
391  for ($i = $begin; $i <= $begin + $plus * 2;$i++)  
392 
393 {  
394 
395 if($i>$this->total_pages)  
396 
397   {  
398 
399   break;  
400 
401 }  
402 
403   if($i == $this->now_page)  
404 
405  {  
406 
407    $return .= "<a class='now_page'>$i</a>\n";  
408 
409  }  
410 
411  else 
412 
413  {  
414 
415  $return .= $this->_get_link($i, $i) . "\n";  
416 
417  }  
418 
419  }  
420 
421   $return .= $this->down_page();  
422 
423 $return .= $this->last_page();  
424 
425   return $return;  
426 
427  }  
428 
429   protected function show_3()  
430 
431   {  
432 
433   $plus = $this->plus;  
434 
435  if( $plus + $this->now_page > $this->total_pages)  
436 
437 {  
438 
439  $begin = $this->total_pages - $plus * 2;  
440 
441  }else{  
442 
443   $begin = $this->now_page - $plus;  
444 
445   }         
446 
447 $begin = ($begin >= 1) ? $begin : 1;  
448 
449 $return = '总计 ' .$this->total_rows. ' 个记录分为 ' .$this->total_pages. ' 页, 当前第 ' . $this->now_page . ' 页 ';  
450 
451   $return .= ',每页 ';  
452 
453  $return .= '<input type="text" value="'.$this->list_rows.'" id="pageSize" size="3"> ';  
454 
455  $return .= $this->first_page()."\n";  
456 
457  $return .= $this->up_page()."\n";   
458 
459  $return .= $this->down_page()."\n";  
460 
461   $return .= $this->last_page()."\n";  
462 
463  $return .= '<select onchange="'.$this->ajax_func_name.'(this.value)" id="gotoPage">';  
464 
465 for ($i = $begin;$i<=$begin+10;$i++)  
466 
467  {  
468 
469  if($i>$this->total_pages)  
470 
471  {  
472 
473   break;  
474 
475   }             
476 
477  if($i == $this->now_page)  
478 
479  {  
480 
481   $return .= '<option selected="true" value="'.$i.'">'.$i.'</option>';  
482 
483  }  
484 
485  else 
486 
487  {  
488 
489  $return .= '<option value="' .$i. '">' .$i. '</option>';  
490 
491 }             
492 
493 }  
494 
495  $return .= '</select>';  
496 
497  return $return;  
498 
499 }  
500 
501 } 
View Code
 1 <?php
 2 //view.php显示页
 3 error_reporting(7);
 4 include "page.php";//分页类库
 5 include("conn.php"); //数据库链接
 6 
 7 /*
 8 *
 9 *数据查询
10 */
11 $query = mysql_query("select * from city");
12 $count = mysql_num_rows($query);
13 
14 //向分页类中传参
15 $options = array ('total_rows' => $count, //总行数
16 'list_rows' => '200' )//每页显示量
17 ;
18 
19 /* 实例化 */
20 $page = new Core_Lib_Page( $options );
21 
22 //然后 在sql语句里面 limit $page->first_row , $page->list_rows
23 $query = mysql_query("select * from city limit $page->first_row , $page->list_rows");
24 
25 //print_r($v);
26 while($v = mysql_fetch_array($query)) {
27     echo $v[Name]."<br />";
28 }
29 
30 echo $page->show (3); //以第三方式输出
31 
32 ?>
View Code

 简单的完整分页类实例三:

  1 <?php  
  2   //page.php
  3  class Page {  
  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      /** 
 12       * 
 13       * __construct是SubPages的构造函数,用来在创建类的时候自动运行. 
 14       * @$each_disNums   每页显示的条目数 
 15       * @nums     总条目数 
 16       * @current_num     当前被选中的页 
 17       * @sub_pages       每次显示的页数 
 18       * @subPage_link    每个分页的链接 
 19       * @subPage_type    显示分页的类型 
 20       * 
 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      function Page($each_disNums, $nums, $current_page, $sub_pages) {  
 27          $this->each_disNums = intval($each_disNums);  
 28          $this->nums = intval($nums);  
 29          if (!$current_page) {  
 30              $this->current_page = 1;  
 31          } else {  
 32              $this->current_page = intval($current_page);  
 33          }  
 34          $this->sub_pages = intval($sub_pages);  
 35          $this->pageNums = ceil($nums / $each_disNums);  
 36          $this->subPage_link =$_SERVER['PHP_SELF']."?page=";;  
 37      }  
 38      /** 
 39       * 照顾低版本 
 40       */  
 41      /*function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_linke) { 
 42          $this->Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link); 
 43      } 
 44  */  
 45     /* 
 46       __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。 
 47      */  
 48      function __destruct() {  
 49          unset ($each_disNums);  
 50          unset ($nums);  
 51          unset ($current_page);  
 52          unset ($sub_pages);  
 53          unset ($pageNums);  
 54          unset ($page_array);  
 55          unset ($subPage_link);  
 56      }  
 57    
 58     /* 
 59       用来给建立分页的数组初始化的函数。 
 60      */  
 61      function initArray() {  
 62          for ($i = 0; $i < $this->sub_pages; $i++) {  
 63              $this->page_array[$i] = $i;  
 64          }  
 65          return $this->page_array;  
 66      }  
 67    
 68     /* 
 69       construct_num_Page该函数使用来构造显示的条目 
 70       即使:[1][2][3][4][5][6][7][8][9][10] 
 71      */  
 72      function construct_num_Page() {  
 73          if ($this->pageNums < $this->sub_pages) {  
 74              $current_array = array ();  
 75              for ($i = 0; $i < $this->pageNums; $i++) {  
 76                  $current_array[$i] = $i +1;  
 77              }  
 78          } else {  
 79              $current_array = $this->initArray();  
 80              if ($this->current_page <= 3) {  
 81                  for ($i = 0; $i < count($current_array); $i++) {  
 82                      $current_array[$i] = $i +1;  
 83                  }  
 84              }  
 85              elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {  
 86                  for ($i = 0; $i < count($current_array); $i++) {  
 87                      $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;  
 88                  }  
 89              } else {  
 90                  for ($i = 0; $i < count($current_array); $i++) {  
 91                      $current_array[$i] = $this->current_page - 2 + $i;  
 92                  }  
 93              }  
 94          }  
 95    
 96         return $current_array;  
 97      }  
 98    
 99     /* 
100      构造普通模式的分页 
101      共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 
102      */  
103      function subPageCss1() {  
104          $subPageCss1Str = "";  
105          $subPageCss1Str .= "共" . $this->nums . "条记录,";  
106          $subPageCss1Str .= "每页显示" . $this->each_disNums . "条,";  
107          $subPageCss1Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";  
108          if ($this->current_page > 1) {  
109              $firstPageUrl = $this->subPage_link . "1";  
110              $prewPageUrl = $this->subPage_link . ($this->current_page - 1);  
111              $subPageCss1Str .= "[<a href='$firstPageUrl'>首页</a>] ";  
112              $subPageCss1Str .= "[<a href='$prewPageUrl'>上一页</a>] ";  
113          } else {  
114              $subPageCss1Str .= "[首页] ";  
115              $subPageCss1Str .= "[上一页] ";  
116          }  
117    
118         if ($this->current_page < $this->pageNums) {  
119              $lastPageUrl = $this->subPage_link . $this->pageNums;  
120              $nextPageUrl = $this->subPage_link . ($this->current_page + 1);  
121              $subPageCss1Str .= " [<a href='$nextPageUrl'>下一页</a>] ";  
122              $subPageCss1Str .= "[<a href='$lastPageUrl'>尾页</a>] ";  
123          } else {  
124              $subPageCss1Str .= "[下一页] ";  
125              $subPageCss1Str .= "[尾页] ";  
126          }  
127    
128         return $subPageCss1Str;  
129    
130     }  
131    
132     /* 
133      构造经典模式的分页 
134      当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页] 
135      */  
136      function subPageCss2() {  
137          $subPageCss2Str = "";  
138          $subPageCss2Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";  
139    
140         if ($this->current_page > 1) {  
141              $firstPageUrl = $this->subPage_link . "1";  
142              $prewPageUrl = $this->subPage_link . ($this->current_page - 1);  
143              $subPageCss2Str .= "[<a href='$firstPageUrl'>首页</a>] ";  
144              $subPageCss2Str .= "[<a href='$prewPageUrl'>上一页</a>] ";  
145          } else {  
146              $subPageCss2Str .= "[首页] ";  
147              $subPageCss2Str .= "[上一页] ";  
148          }  
149    
150         $a = $this->construct_num_Page();  
151          for ($i = 0; $i < count($a); $i++) {  
152              $s = $a[$i];  
153              if ($s == $this->current_page) {  
154                  $subPageCss2Str .= "[<span style='color:red;font-weight:bold;'>" . $s . "</span>]";  
155              } else {  
156                  $url = $this->subPage_link . $s;  
157                  $subPageCss2Str .= "[<a href='$url'>" . $s . "</a>]";  
158              }  
159          }  
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          }  
170          return $subPageCss2Str;  
171      }  
172  }    
173   
174 
175  ?>  
View Code
 1 <?php
 2 //view.php显示页
 3 error_reporting(7);
 4 include "page.php";//分页类库
 5 include("conn.php"); //数据库链接
 6 
 7 /*
 8 *
 9 *数据查询
10 */
11 $query = mysql_query("select * from city");
12 $nums = mysql_num_rows($query);//总条目数
13 $each_disNums=200; //每页显示的条目数   
14 $sub_pages=5; //每次显示的页数 
15 
16 
17 $page_all_num = ceil($nums/$each_disNums); //总页数
18 $current_page=empty($_GET['page'])?1:$_GET['page']; //当前页数
19 $current_page=(int)$current_page; //安全强制转换 
20 $limit_st = ($current_page-1)*$each_disNums; //起始数
21 //echo $limit_st;
22 //die;
23 $query_sub1 = mysql_query("select * from city limit $limit_st, 200");
24 //print_r($v);
25 while($v = mysql_fetch_array($query_sub1)) {
26   echo $v[Name]."<br />";
27 }
28   
29  
30  $t = new Page(200, $nums, $current_page,$sub_pages); //5表示显示5个页码供选择 
31  echo $t->subPageCss1();  
32 // echo "<br>";  
33  //echo $t->subPageCss2(); 
34 ?>
View Code

 

posted on 2014-05-16 21:07  lbsf  阅读(196)  评论(0)    收藏  举报

导航