动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   PHP → 阅读文章

 自己写一个分页显示的类

作者来源: 
阅读 1593 人次 , 2006-4-19 9:31:00 


   class.php:
<?
/*
-----------------------------------------------------------------------------------------------
名称:TurnPage
作用:分页显示
成员函数:
Entrance():类的入口,参数是所有成员函数的参数
SelectData($connection,$query):选择数据的函数,返回一个数组
ShowData($array,$i,$j):显示各个字段值的函数,返回一个值
MakeTable($array,$intPageBegin,$intPageEnd):生成表格的函数,根据要显示的字段的个数生成相应的表格,参数分别是:数组,每页开始的信息序号,每页结束的信息序号
MakeLink($parameter=null):显示翻页的链接,参数可选
GetFileName(): 取出当前执行文件名字,返回文件名

函数之间的关系:
-----------------------------------------------------------------------------------------------
*/
class TurnPage
{
var $strArray;
var $cols;
var $rows;
var $strFieldName;
var $intPageBegin;
var $intPageEnd;

function Entrance($connection,$query)
{
$myArray=$this->SelectData($connection,$query);

$this->GetFileName();

$this->MakeLink($parameter=null);


$this->MakeTable($myArray,$this->intPageBegin,$this->intPageEnd);

}

function SelectData($connection,$query)
{
$result=@mysql_query($query,$connection) or die("unable to select the data!");

$intFieldsNum=mysql_num_fields($result);

for($a=0;$a<$intFieldsNum;$a++) //取得选择的各个字段的名字
{
$this->strFieldName[$a]=mysql_field_name($result,$a);
}

$this->cols=$intFieldsNum;

$count=0;

while($rows=mysql_fetch_row($result))
{
for($i=0;$i<$intFieldsNum;$i++)
{
$data[$count][$i]=trim($rows[$i]);
}
$count++;
}
$this->rows=count($data);
$this->strArray=$data;
return $this->strArray;
}

function ShowData($array,$i,$j)
{
return $array[$i][$j];
}

function MakeTable($array,$intPageBegin,$intPageEnd)
{
echo "<table border=0 width=100% align=center cellspacing=1 cellpadding=5 bgcolor=#004080>";
echo "<tr height=20 bgcolor=#0080C0 align=center>";
for($m=0;$m<$this->cols;$m++)
{
echo "<td><font color=white>".$this->strFieldName[$m]."</font></td>";
}
echo "</tr>";

for($i=$intPageBegin;$i<$intPageEnd;$i++)
{
if ($i%2==0)
{
$bgColor="#D2F0FF";
}
else
{
$bgColor="#ffffff";
}
echo "<tr bgcolor=".$bgColor." align=center onMouseOver=this.style.backgroundColor='#FFCC66' onMouseOut=this.style.backgroundColor='' onClick=this.style.backgroundColor='#cccccc'>";
for($j=0;$j<$this->cols;$j++)
{
echo "<td>".$this->ShowData($array,$i,$j)."</td>";
}
echo "</tr>";
}
echo "</table>";
}

function GetFileName()
{
$strPath=$_SERVER['PHP_SELF'];
$strFileName=trim(substr($strPath,(strrpos($strPath,"/")+1)));
return $strFileName;
}

function MakeLink($parameter=null)
{
if($parameter==null)
{
$strParam=null;
}
else
{
$strParam=$parameter;
}

if($_GET["intPage"]==null)
{
$intPage=1;
}
else
{
$intPage=trim($_GET['intPage']);
}

$intPerPage=10;

$total=$this->rows;

$strFile=$this->GetFileName();

$intPageBegin=$intPerPage*$intPage-$intPerPage;

$this->intPageBegin=$intPageBegin;



if ($intPage*$intPerPage<$total)
{
$intPageEnd=$intPageBegin+$intPerPage;
}
else
{
$intPageEnd=$total;
}

$this->intPageEnd=$intPageEnd;

echo "<table width=100% border=0><tr><td width=60% align=left>信息总数:".$this->rows."</td><td width=20%>&nbsp;";
if($intPage>1)
{
echo "<a href=".$strFile."?intPage=".($intPage-1).">上一页</a>";
}
echo "</td><td width=20%>&nbsp;";
if ($intPage*$intPerPage<$total)
{
echo "<a href=".$strFile."?intPage=".($intPage+1).">下一页</a>";
}
echo "</td></tr></table>";

}

}
?>
test.php:
<?
include("../common/connection.php"); //连接数据库的操作
include("../common/class.php"); //把上面的文件包含近来

$query="select * from uphoto order by intUID asc";

$test=new TurnPage;

$test->Entrance($connection,$query);

?>   
 本文Tags分页  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:ImageList——图片列表类
· 下一篇:PHP自动生成月历代码共享
· PHP比ASP优秀的七个理由
· 浅谈PHP语法(一)
· 打造计数器DIY三步曲(下)
· 上一主题/下一主题的实现
· 在PHP世界中选择最合适的模板--比较PHPLIB Template和FastTemplate


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077