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%> ";
if($intpage>1)
{
echo "<a href=".$strfile."?intpage=".($intpage-1).">上一页</a>";
}
echo "</td><td width=20%> ";
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);
?>
