php/** PHP version:5.x
*功能:能够将指定的表的数据与表格绑定,带分页功能,点击指定字段排序,点击指定编号删除记录,适合后台管理
*属性:
*$bgcolor....................................................表格背景颜色
*$mytable....................................................指定数据库中的表
*$pageSize.................................................每页面显示记录条数
*$totalPage.................................................总页数
*$totalNum...................................................总的记录数量
*$currentPage.............................................起始页
*$start...........................................................limit分页使用的参数
*$page.........................................................页面列表
*$firstFIled.....................................................表中的第一个字段名(一般为ID或者类似的字段)
*$actionPage.................................................删除指定记录的脚本页面,默认为del.php,若指定了脚本页面,前提是必须存在
*
*方法
*__construct($tables,$order,$pageSize=10,$actionPage="del.php")................................构造函数参数一表示要绑定的表的名字,参数二表示默认的排序字段,参数三表示每页显示多少条记录,参数四表示数据删除的脚本处理页面,默认为del.php
*private function notice()..................................私有的方法 ,主要输出操作提示区域,也可以输出其他信息
*private function pages()..................................分页链接显示区域
*private function splitePage()..........................获取分页所需的所有参数
*private function getPages()............................获取分页链接
*public function outputPage().........................输出最后的结果
*private function getOrders()............................获取排序字段
*public function subString($content,$length=20)........................................截取超过指定长度的字符串,参数一为字符串,参数二为指定长度
*private function getFirstFiled().....................................获取表中的第一个字段
*public function delRecords().......................................删除记录
*/class dataBind{
public $bgColor;//指定表格背景颜色public $mytable;//指定要绑定的表public $pageSize,$totalPage,$totalNum,$currentPage,$start,$page;//分页所需的属性public $message;//设定操作提示public $order,$firstFiled;//排序字段public $actionPage;
public $desc;//升序或者降序
//构造函数,初始化基本属性function __construct($tables,$order,$pageSize=10,$actionPage="del.php")
{$this->bgColor="orange";$this->mytable=$tables;//即将使用的表$this->order=$order;//设定默认的排序字段,前提是表中已经存在的字段$this->pageSize=$pageSize;//每页显示条数$this->currentPage=1;self::getOrders();//获取排序参数self::getPages();//获取页数$this->desc="desc";//默认降序排列$this->start=($this->currentPage-1)*$this->pageSize;//使用limit分页时的起始记录的编号 $this->message="提示:点击第一行的每一列可以按照该列排序,双击击每一行第一列可以删除该记录";//操作提示信息$this->firstFiled=self::getFirstFiled();//获取表中的第一个字段名$this->actionPage="$actionPage";
}
//操作提示信息区private function notice()
{
echo "
$this->message
";
}
//分页链接显示区域private function pages()
{
for ($m=1;$m<=$this->totalPage;$m++)
{
if($this->currentPage==$m)
{$this->page.="$m";//如果是当前页就不显示为链接}
else
{$this->page.="<order>$m>  ";
}
}
echo "Total pages :$this->totalPage Page:$this->page
";
}
//获取分页所需要的所有参数的值private function splitePage()
{$sql="select * from $this->mytable";$res=mysql_query($sql) or die(mysql_error());$this->totalNum=mysql_num_rows($res);$this->totalPage=ceil($this->totalNum/$this->pageSize);//总页数
}
//获取翻页链接private function getPages()
{
if($_GET["page"]==""||$_GET["page"]<0)
{$this->currentPage=1;
}
else
{$this->currentPage=$_GET["page"];
}
}
//输出 public function outputPage()
{
self::notice();self::splitePage();$sql="select * from $this->mytable order by '$this->order' $this->desc limit $this->start,$this->pageSize";$res=mysql_query($sql) or die(mysql_error());$rows=mysql_num_rows($res);$rs=mysql_fetch_array($res ,1);
echo "";
echo "";//输出表格的第一行,就是数据库中表的字段名foreach ($rs as $key=>$values)
{
echo "$key ";
}
echo " ";
//输出所有的数据for ($i=0;$i<$rows;$i++)
{
echo "";
foreach ($rs as $key=>$value)
{$value=self::subString($value,40);
if($key==$this->firstFiled)
{//为表格的每行的第一列设定动作echo "$value ";
}
else
{
echo "$value ";
}
}
echo " ";$rs=@mysql_fetch_array($res,1);
}
echo "
";self::pages($this->totalPage,$this->page);self::Ifream();
}
//获取排序字段private function getOrders()
{
if($_GET["key"]!="")
{$this->order=$_GET["key"];
}
}
//截取字符串,诸如文章内容public function subString($content,$length=20)
{
if(strlen($content)>$length)
{$content=substr($content,0,$length)."...";
return $content;
}
else
{
return $content;
}
}
//获取第一个字段private function getFirstFiled()
{$sql="select * from $this->mytable";$res=mysql_query($sql) or die(mysql_error());$rs=mysql_fetch_array($res,1);$firstFiled=array_keys($rs);
return $firstFiled[0];
}
//输出一个隐藏的ifream,有些链接的方向或表单的提交方向都指向他private function Ifream()
{
echo << IF;
}//删除记录public function delRecords()
{$ID=$_GET["$this->firstFiled"];$sql="delete from $this->mytable where $this->firstFiled=$ID";mysql_query($sql) or die(mysql_error());
return true;
}
}?> 2.Demo.phpPHP代码:
<script language=javascript src=include/order.js>
include_once("include/date.class.php");//$conn=mysql_connect("localhost","root","123456");mysql_select_db("test");$db=new dataBind("bbs","T_ID",5,"del.php");$db->outputPage();?> 3.del.php
PHP代码:
phpinclude_once("include/date.class.php");mysql_connect("localhost","root","123456");mysql_select_db("test");$db=new dataBind("bbs","ID",5,"{$_SERVER['PHP_SELF']}");//注意第二个参数,必须是参数一中确实存在的字段if($db->delRecords())
{
echo "";
}
else
{
echo "";
}?> 4.order.jsPHP代码:
function orders(phpself,page,key)
{location.href=phpself+"?page="+page+"&key="+key;
}
function command(url)
{document.getElementById("Command").src=url;
}
function message(msg)
{alert(msg);
}
function reload()
{top.location.reload();
} 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/786540/viewspace-927688/ ,如需转载,请注明出处,否则将追究法律责任。