Chinaunix
标题:
php5实现的数据分页类
[打印本页]
作者:
sleep_meng
时间:
2004-05-15 00:08
标题:
php5实现的数据分页类
本类是把数组中的数据分页显示出来,
如要显示数据库中的数据,先把数据库中的数据倒入数组。
本人学php才几个月,请大家多批评一下。
下面是分页类和把数据库中数据倒入数组的类源码。
分页类:
$forStartCtrl 和 $forEndCtrl 两个变量是在显示数据时用来控制for循环.
<?php
/***************************************************
[UPLINK_SKY] index.php --Meng LinuxRuby.
This is the freeware.
Version : 0.1.
Author : Meng LinuxRuby (sleep_linux@163.com).
CopyRight: (C)2004.05.* Meng LinuxRuby.
***************************************************/
class Page{
private $countOfPage;
private $isThisPage;
private $isFirstPage;
private $isEndPage;
private $prevPage;
private $nextPage;
private $numOfData;
private $pageSize = 10;
private $forStartCtrl;
private $forEndCtrl;
function __construct($dataArray){
if (!empty($_GET['page'])){
$this->;isThisPage = $_GET['page'];
}else{
$this->;isThisPage = 1;
}
if (!empty($_GET['pageSize']) || !empty($_POST['pageSize'])){
if (!empty($_GET['pageSize'])){
$this->;pageSize = $_GET['pageSize'];
}elseif(!empty($_POST['pageSize'])){
$this->;pageSize = $_POST['pageSize'];
}
}
try{
$this->;numOfData = count($dataArray);
if ($this->;numOfData >; 0){
$this->;countOfPage = ceil($this->;numOfData / $this->;pageSize);
}else{
$this->;countOfPage = 1;
}
}catch(Exception $exception){
print $exception->;getMessage();
}
if ($this->;countOfPage == 1){
$this->;isFirstPage = true;
$this->;isEndPage = true;
}elseif ($this->;isThisPage == 1){
$this->;isFirstPage = true;
$this->;isEndPage = false;
}elseif ($this->;isThisPage == $this->;countOfPage){
$this->;isFirstPage = false;
$this->;isEndPage = true;
}else{
$this->;isFirstPage = false;
$this->;isEndPage = false;
}
if ($this->;countOfPage >; 1){
if (!$this->;isFirstPage){
$this->;prevPage = $this->;isThisPage - 1;
}
if (!$this->;isEndPage){
$this->;nextPage = $this->;isThisPage + 1;
}
}
$this->;forStartCtrl = ($this->;isThisPage - 1) * $this->;pageSize;
if (($this->;isThisPage * $this->;pageSize) >; $this->;numOfData){
$this->;forEndCtrl = $this->;numOfData;
}else{
$this->;forEndCtrl = $this->;isThisPage * $this->;pageSize;
}
return true;
}
function getStartCtrl(){
return $this->;forStartCtrl;
}
function getEndCtrl(){
return $this->;forEndCtrl;
}
function showPageTag(){
if ($this->;isFirstPage){
$tag = "首页|上一页|";
}else{
$tag = "<a href='?page=1'>;首页</a>;|<a href='?page=".$this->;prevPage."'>;上一页</a>;|";
}
if ( $this->;isEndPage ){
$tag .= "下一页|尾页";
}else{
$tag .= "<a href='?page=".$this->;nextPage."'>;下一页</a>;|<a href='?page=".$this->;countOfPage."'>;尾页</a>;";
}
print $tag." 当前第".$this->;isThisPage."页 总共".$this->;countOfPage."页";
}
}
?>;
复制代码
把MySQL数据库数据倒入数组类,如用别的数据库,请修改相关代码。
<?php
/***************************************************
[UPLINK_SKY] mysql.class.php --Meng LinuxRuby.
This is the freeware.
Author : Meng LinuxRuby (sleep_linux@163.com).
CopyRight: (C)2004.05.* Meng LinuxRuby.
***************************************************/
class mysqlDB {
private $sqlResult = array();
private $sqlLink;
function __construct($dbHost, $dbUser, $dbPass, $dbBase){
try{
$this->;sqlLink = MYSQL_CONNECT($dbHost, $dbUser, $dbPass);
MYSQL_SELECT_DB($dbBase);
}catch(Exception $exception){
print "Can't Connect to MySQL Database";
print $exception->;getMessage();
}
}
function fetchArray($sqlString){
try{
if ($Rs = MYSQL_QUERY($sqlString, $this->;sqlLink)){
while ($result = MYSQL_FETCH_ARRAY($Rs)){
$this->;sqlResult[] = $result;
}
return $this->;sqlResult;
}else{
return false;
}
}catch(Exception $exception){
print $exception->;getMessage();
}
}
function __destruct(){
try{
return MYSQL_CLOSE($this->;sqlLink) ? TRUE:FALSE;
}catch(Exception $exception){
print $exception->;getMessage();
}
}
}
?>;
复制代码
使用范例:
$page = new Page($dataArray);
for ($i=$page->;getStartCtrl();$i<$page->;getEndCtrl();$i++){
//显示数据
echo "<br>;".$dataArray[$i]['User_ID'];
echo "<br>;".$dataArray[$i]['User_Pass'];
echo "<br>;".$dataArray[$i]['User_Auth'];
}
//页标
print $page->;showPageTag();
复制代码
作者:
夜猫子
时间:
2004-05-15 00:19
标题:
php5实现的数据分页类
建议以后发代码用[code][/code]把代码框起来,这样其他人比较容易浏览
作者:
tonera
时间:
2004-05-15 00:43
标题:
php5实现的数据分页类
前赴后继!
看来PHP朝全面面向OO进军了。呵呵,大家都在搞,形势一片大好,全国山河一片红啊。
不错。
作者:
tonera
时间:
2004-05-15 00:43
标题:
php5实现的数据分页类
前赴后继!
看来PHP朝全面面向OO进军了。呵呵,大家都在搞,形势一片大好,全国山河一片红啊。
不错。
作者:
tonera
时间:
2004-05-15 00:43
标题:
php5实现的数据分页类
前赴后继!
看来PHP朝全面面向OO进军了。呵呵,大家都在搞,形势一片大好,全国山河一片红啊。
不错。
作者:
yb0312
时间:
2004-05-15 17:27
标题:
php5实现的数据分页类
什么是oo?
作者:
sleep_meng
时间:
2004-05-15 18:01
标题:
php5实现的数据分页类
OO是面向对像,OOP表示面向对像编程
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2