mysql+php分页类(已测)


Posted in PHP onMarch 31, 2008
<?php       
/*      
mysql_pager.class.php      三个参数。 mysql_query()的结果, url变量page, 您要的每页记录数      
例子在这个文件底部      
淡水河边整理测试      
*/       
class mysql_pager {       
  // define properties       
  var $page;       
  var $result;       
  var $results_per_page = 3;       
  var $total_pages;         
/*        
Define the methods      
  下面是构造函数,和类同名(>php4)      
   需要查询的结果句柄,当前页码,每页记录数      
   like:  $f->mysql_pager($result, 1, 15);      
*/       
function mysql_pager( $result, $current_page, $results_per_page ) {       
    if(!$result){       
        echo "<div align=center>数据库未运行,结果集错误</div>\n";         
        return;       
        }       
   $this->result = $result;       
   if(!$current_page || $current_page < 0)         
        $this->page = 1;       
   else $this->page = $current_page;       
   if(!emptyempty($results_per_page))         
      $this->results_per_page = $results_per_page;       
   $numrows = @mysql_num_rows($this->result);         
   if(!$numrows) {       
      echo "<div align=center>查询结果为空.</div>\n";       
      return;       
      }       
   $this->total_pages = ceil($numrows / $this->results_per_page);         
}       
/*        
  下面是打印内容的函数,可以不用,也可以根据自己的需要扩展      
  这里只是打印出id      
*/       
function print_paged_results() {         
    echo "<table border=0 align=center>\n";       
    $start = ($this->page - 1) * $this->results_per_page;       
    mysql_data_seek($this->result, $start);       
    $x = 0;       
    for($i = 1; $i <= $this->results_per_page && $row = @mysql_fetch_array($this->result); $i++) {       
        if($x++ & 1) $bgcolor = "#F2F2FF";       
        else $bgcolor = "#EEEEEE";       
        echo "<tr bgcolor=$bgcolor><td>". $row["id"] . "</td></tr>";             
        // 编辑这部分输出任何您想要的HTML       
        }       
       echo "</table>\n";       
}       
/*        
  下面是打印页码和链接的函数      
  在我们需要显示页码的地方调用      
*/       
function print_navigation() {       
    global $PHP_SELF;       
    echo "<div align=center>";       
    for($i = 1; $i <= $this->total_pages; $i++) { #loop to print << 1 2 3... $total_pages >>         
       if($i == 1 && $this->page > 1)  #Prints the << first to goto the previous page (not on page 1)         
          echo "<a href=\"$PHP_SELF?page=".($this->page - 1)."\" onMouseOver=\"status="Previous Page";return true;\" onMouseOut=\"status=" ";return true;\">?</a>";         
       if($i == $this->page)  #Doesn"t print a link itself, just prints page number        
          echo "<font color=\"#ff3333\"> $i </font>";         
       if($i != $this->page)  #Other links that aren"t this page go here        
          echo "<a href=\"$PHP_SELF?page=$i\" onMouseOver=\"status="Go to Page $i";return true;\" onMouseOut=\"status=" ";return true;\"> $i </a>";         
       if($i == $this->total_pages && $this->page != $this->total_pages)  #  Link for next page >>  (not on last page)         
          echo "<a href=\"$PHP_SELF?page=".($this->page + 1)."\" onMouseOver=\"status="Go to the Next Page";return true;\" onMouseOut=\"status=" ";return true;\">?</a>";         
       }       
    echo "</div>\n";         
  }       
}       
/*      
   mysql_connect($server, $uname, $pass );      
   mysql_select_db("$db");      
   $result= @mysql_query("Select * FROM table");      
   $p = new mysql_pager( $result, $page=$_GET["page"], 10 );      
   $p->print_navigation();      
   $p->print_paged_results();      
   $p->print_navigation();      
*/       
?> 
PHP 相关文章推荐
php学习之数据类型之间的转换代码
May 29 PHP
第二章 PHP入门基础之php代码写法
Dec 30 PHP
超小PHP小马小结(方便查找后门的朋友)
May 05 PHP
php 模拟GMAIL,HOTMAIL(MSN),YAHOO,163,126邮箱登录的详细介绍
Jun 18 PHP
浅析关于PHP位运算的简单权限设计
Jun 30 PHP
ThinkPHP调用common/common.php函数提示错误function undefined的解决方法
Aug 25 PHP
PHP实现linux命令tail -f
Feb 22 PHP
PHP程序中的文件锁、互斥锁、读写锁使用技巧解析
Mar 21 PHP
php使用curl并发减少后端访问时间的方法分析
May 12 PHP
PHP实现的多进程控制demo示例
Jul 22 PHP
Laravel框架中集成MongoDB和使用详解
Oct 17 PHP
PHP使用POP3读取邮箱接收邮件的示例代码
Jul 08 PHP
PHP 数字左侧自动补0
Mar 31 #PHP
加强版phplib的DB类
Mar 31 #PHP
PHP截取汉字乱码问题解决方法mb_substr函数的应用
Mar 30 #PHP
PHP5中的时间相差8小时的解决办法
Mar 28 #PHP
php heredoc和phpwind的模板技术使用方法小结
Mar 28 #PHP
WINDOWS下php5.2.4+mysql6.0+apache2.2.4+ZendOptimizer-3.3.0配置
Mar 28 #PHP
Mysql的GROUP_CONCAT()函数使用方法
Mar 28 #PHP
You might like
深入了解 register_globals (附register_globals=off 网站打不开的解决方法)
2012/06/27 PHP
php根据用户语言跳转相应网页
2015/11/04 PHP
PHP批量删除jQuery操作
2017/07/23 PHP
Angular 理解module和injector,即依赖注入
2016/09/07 Javascript
jQuery插入节点和移动节点用法示例(insertAfter、insertBefore方法)
2016/09/08 Javascript
jQuery实现联动下拉列表查询框
2017/01/04 Javascript
基于JavaScript表单脚本(详解)
2017/10/18 Javascript
vue加载自定义的js文件方法
2018/03/13 Javascript
vue弹窗组件使用方法
2018/04/28 Javascript
深入浅析Node环境和浏览器的区别
2018/08/14 Javascript
解决layer图标icon不加载的问题
2019/09/04 Javascript
微信h5静默和非静默授权获取用户openId的方法和步骤
2020/06/08 Javascript
vue-cli中实现响应式布局的方法
2021/03/02 Vue.js
在Python中通过getattr获取对象引用的方法
2019/01/21 Python
Python requests模块实例用法
2019/02/11 Python
Python一个简单的通信程序(客户端 服务器)
2019/03/06 Python
python itchat实现调用微信接口的第三方模块方法
2019/06/11 Python
pyqt5 comboBox获得下标、文本和事件选中函数的方法
2019/06/14 Python
python中列表的切片与修改知识点总结
2019/07/23 Python
PyQt5+Caffe+Opencv搭建人脸识别登录界面
2019/08/28 Python
Python实现二叉树的最小深度的两种方法
2019/09/30 Python
PyTorch实现更新部分网络,其他不更新
2019/12/31 Python
pytorch加载自己的图像数据集实例
2020/07/07 Python
html5 canvas绘制放射性渐变色效果
2018/01/04 HTML / CSS
J.Crew官网:美国知名休闲服装品牌
2017/05/19 全球购物
国际知名军事风格休闲装品牌:Alpha Industries(阿尔法工业)
2017/05/24 全球购物
澳洲Chemist Direct药房中文网:澳洲大型线上直邮药房
2019/11/04 全球购物
服装厂厂长职责
2013/12/16 职场文书
校运动会广播稿(100篇)
2014/09/12 职场文书
个人融资协议书范本两则
2014/10/15 职场文书
公司领导班子召开党的群众路线教育实践活动总结大会新闻稿
2014/10/21 职场文书
2014年幼儿园保育工作总结
2014/12/02 职场文书
地方课程教学计划
2015/01/19 职场文书
单位工作证明范本
2015/06/15 职场文书
Golang 语言控制并发 Goroutine的方法
2021/06/30 Golang
源码分析Redis中 set 和 sorted set 的使用方法
2022/03/22 Redis