• ----:)欢迎访问源码网(:----
    • 首页
    • 博客
    • 学院
    • 下载
    • 论坛
    • 影视
    • 发布源码
    • RSS
    • ITPig
    • 笑话网
    • 百家姓
    • 繁體中文

源码网 - 中国第一源码门户
选择镜像:网通镜像 - 电信主站
  • 首 页
  • 新闻动态
  • 网站运营
  • 网页制作
  • WEB开发
  • 编程开发
  • 图像媒体
  • 操作系统
  • 数据库
  • 服务器
热门搜索 优化 SEO 故事 cms IIS7 MySQL 个人 AdSense 主题推广 | 文章搜索: 高级搜索
会员登录/控制面版您的位置: 学院首页 >> WEB开发 >> PHP 开发 >> PHP代码 >> 详细内容
 

推荐文章

 
 

热点文章

  • 3389远程服务器GHOST的视频教程
  • 利用纯真QQIP数据库做快速IP归属地查询
  • 天气预报小偷,根据IP自动判断地址
  • php在线文本编辑器
  • 实例(Smarty+FCKeditor新闻系统)
  • php里实现汉字转区位码
  • 对dvbbs.php 全文搜索的完全分析
  • php生成会动的gif图片代码
  • php的字符编码转换工具
  • DIV+CSS+PHP巨献——网页内容先竖排再横排
  • PHP获取网卡的MAC地址
  • 56.com视频采集接口程序(PHP)
 
 

相关文章

  • PHP初级程序员面试题及答案
  • PHP使用zlib扩展实现页面GZIP压缩输出
  • 为什么PHP令人不爽(对于大型系统)
  • php中的stdClass
  • php中ADODB类使用
  • php自动post数据--百度贴吧灌水机器人代码实例
  • Linux/FreeBSD下用C语言开发PHP的so扩展模块例解
  • php取不到session的常见原因
  • 使用php的zlib压缩和解压缩swf文件
  • php 做服务器端程序
  • PHP5.2下function和class性能对比
  • PHP用mkdir()新建立目录无写的权限的问题
 
 

百度搜索

 
 

PHP,MySQL,模板分页类

  • 阅览次数:
  • 文章来源: CP整理
  • 原文作者:
  • 整理日期: 2008-08-13
  • 发表评论
  • 字体大小:
  • 小
  • 中
  • 大

<?php  
class Pages{  
    
//数据源
    
var $sql;
    
//当前页的页码
    
var $currentPageNumber;
    
//每页的数据条数
    
var $itemsPerPage;
    
//调用的地址URL
    
var $url;
    
//总页数
    
var $totalPages;
    
//数据总数
    
var $totalNums;
    
    
//显示模块的前缀代码
    
var $model_block_pre;
    
//显示模块的后缀代码
    
var $model_block_end;
    
//显示循环数据的前缀代码
    
var $model_line_pre;
    
//显示循环数据的后缀代码
    
var $model_line_end;
    
//显示循环数据的主模版
    
var $model_main;
    
//模版参数
    
var $model_argvs;
    
    
//初始化函数
    //$sql,数据源SQL
    //$currentPageNumber,当前页的页码
    //$url,调用的地址URL
    //$model,用于显示的数据模版
    //$itemsPerPage,每页的数据条数
    
function Pages($sql,$currentPageNumber,$url,$itemsPerPage = 10){  
        
$this->sql = $sql;
        
$this->currentPageNumber = $currentPageNumber;
        
$this->itemsPerPage = $itemsPerPage;
        
$this->url = $url;
        
$this->url.=(stristr($this->url,'?')!=false)?'&':'?';
        
$this->get_total();
        
$this->get_safe_pages();
    }  
    
    
//获取总数和总页数
    
function get_total(){  
        
$result = mysql_query($this->sql) or die("SQL Error:" . mysql_error());
        
$totalNums = mysql_num_rows($result);
        
$totalPages = ceil($totalNums/$this->itemsPerPage);
        
$this->totalNums = $totalNums;
        
$this->totalPages = $totalPages;
        
mysql_free_result($result);
    }  
    
    
//将当前页保持在安全的范围
    
function get_safe_pages(){  
        if (
$this->currentPageNumber > $this->totalPages){  
            
$this->currentPageNumber = $this->totalPages;
        }elseif (
$this->currentPageNumber < 1){  
            
$this->currentPageNumber = 1;
        }  
    }  
    
    
//获取当前页的数据起始位置
    
function get_limit_start(){  
        
$limit_start = ($this->currentPageNumber - 1) * $this->itemsPerPage;
        return
$limit_start;
    }  
    
    
//获取当前页的数据数目
    
function get_limit_nums(){  
        return
$this->itemsPerPage;
    }  
  
    
//获取当前页的数据
    
function get_data(){  
        
$SQL = $this->sql . " limit " . $this->get_limit_start() . "," . $this->get_limit_nums();
        
$result = mysql_query($SQL) or die("SQL Error:" . mysql_error());
        return
$result;
    }  
    
    
//检查主模版并且提取参数
    
function check_main_model(){  
        
$temparray =split('\[',$this->model_main);
        
$returnarray = array();
        for(
$i=1;$i<count($temparray);$i++){  
            
$tempstr = substr($temparray[$i],0,stripos($temparray[$i],']'));
            
$templenth = strlen( $tempstr );
            if (
$templenth != 0){  
                
$returnarray[] = $tempstr;
            }  
        }  
        
$this->model_argvs = $returnarray;
        if (
count($returnarray) == 0 )return false;else return true;
    }  
    
    
//根据模版显示数据
    
function show_data($model){  
        
$this->model_block_pre = $model[0];
        
$this->model_block_end = $model[1];
        
$this->model_line_pre = $model[2];
        
$this->model_line_end = $model[3];
        
$this->model_main = $model[4];
        if (
$this->totalNums < 1){  
            echo
'没有数据';
        }elseif (
$this->check_main_model() == false){  
            echo
'模版错误';
        }else{  
            echo
$this->model_block_pre;
            
$result = $this->get_data();
            while (
$row = mysql_fetch_assoc($result) ) {  
                
$tempstr = $this->model_main;
                echo
$this->model_line_pre;
                for(
$i=0; $i < count($this->model_argvs);$i++){  
                    
$preg = "(.*)(\[". $this->model_argvs[$i] ."\])(.*)";
                    
$target = "\\1". $row[$this->model_argvs[$i]] ."\\3";
                    
$tempstr = ereg_replace($preg,$target,$tempstr);
                }  
                echo
$tempstr;
                echo
$this->model_line_end;
            }  
            echo
$this->model_block_end;
        }  
    }  
    
    
//显示分页项目
    
function show_pages(){  
        if (
$this->totalNums < 1){  
            
//没有数据
        
}else{  
            echo
"<p>";
            echo
"<a href=\"".$this->url."page=1\">首页</a> ";
            echo
"<a href=\"".$this->url."page=". ($this->currentPageNumber - 1) ."\">上页</a> ";
            echo
"<a href=\"".$this->url."page=". ($this->currentPageNumber + 1) ."\">下页</a> ";
            echo
"<a href=\"".$this->url."page=". ($this->totalPages) ."\">尾页</a> ";
            echo (
$this->currentPageNumber) ."/". ($this->totalPages) ." ";
            echo
"每页". ($this->itemsPerPage) ."条/共". ($this->totalNums) ."条";
            echo
"</p>";
        }  
    }  
}  
?>

调用要这样
PHP: 

$model = array(  
//模版头
'<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
<td width="25%">ID</td>
<td width="25%">姓名</td>
<td width="25%">问题</td>
<td width="25%">答案</td></tr>'
,
//模版尾
'</table>',
//模版循环头
'<tr class="tt">',
//模版循环尾
'</tr>',
//模版主体,其中方括号中间的部分为数据库相应的字段名称
'<td>[id]</td><td>[name]</td><td>[question]</td><td>[answered]</td>'  
);
$pages= new Pages($sql,(int)$_GET['page'],$_SERVER['PHP_SELF']);
//根据模版显示数据
$pages->show_data($model);
//显示相应的页码
$pages->show_pages();


上一篇:PayPal全中文详解
下一篇:PHP初级程序员面试题及答案
  • 网友评论:
  • 查看所有评论
  • 我要发表评论
您的网名:
留言主题:
你要发表的内容:

 

关于本站 | 广告联系 | 版权声明 | 网站地图 | 发布软件 | 帮助中心 | 源码论坛

Copyright © 2005-2007 CodePub.Com  程序支持:木翼  滇ICP备05005971号