+ -
当前位置:首页 → 问答吧 → 仿照.net写的表格数据绑定的类(排序,删除,分页功能)

仿照.net写的表格数据绑定的类(排序,删除,分页功能)

时间:2007-07-15

来源:互联网

学了php这么久,最近写后台的管理的时候写了这个玩意,这个只适合后台管理

简单的说下功能:1.点击字段名排序

2.点击编号删除

3.分页显示功能

附件里面有演示文档以及说明文档,希望大家不吝赐教!

1.文件名:date.class.php
复制PHP内容到剪贴板
PHP代码:

<?php
/*
*author:还珠楼主
*Email:[email protected]
*QQ:150584840
*
* 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 "<table width=100% height=50 cellspacing=1 cellsading=0 border=0 bgcolor=orange style='margin-top:25' align=center><tr>
<td bgcolor=white align=center >$this->message</td></tr></table>";
    }
    
    //分页链接显示区域
    private function pages()
    {
        for ($m=1;$m<=$this->totalPage;$m++)
        {
            if($this->currentPage==$m)
            {
                $this->page.="<font color=red>$m</font>";//如果是当前页就不显示为链接
            }
            else
            {
                $this->page.="<<a href=?page=$m&key=$this->order>$m</a>>&nbsp&nbsp";
            }
        }
        echo "<table width=100% height=50 cellspacing=1 cellsading=0 border=0 bgcolor=orange style='margin-top:25' align=center><tr><td bgcolor=white align=center>Total pages :$this->totalPage  Page:$this->page</td></tr></table>";
    }
    
    //获取分页所需要的所有参数的值
    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 "<table  cellspacing=1 cellspading=0 border=0 bgcolor=orange width=100% style='margin-top:10'>";
        echo "<tr>";
        //输出表格的第一行,就是数据库中表的字段名
        foreach ($rs as $key=>$values)
        {
            echo "<td bgcolor=white id=$key align=center height=35 onclick=\"orders('{$_SERVER['PHP_SELF']}',$this->currentPage,'$key')\" style=\"cursor: pointer; \"  onmouseover=\"this.style.backgroundColor='red'\" onmouseout=\"this.style.backgroundColor='white' \">$key</td>";        
        }
        echo "</tr>";

        //输出所有的数据
        for ($i=0;$i<$rows;$i++)
        {
            echo "<tr>";
            foreach ($rs as $key=>$value)
            {
                $value=self::subString($value,40);
                if($key==$this->firstFiled)
                {
                    //为表格的每行的第一列设定动作
                    echo "<td align=center bgcolor=white height=25 onmouseover=\"this.style.backgroundColor='#EEEEEE'\" onmouseout=\"this.style.backgroundColor='white' \"  ondblclick=\"command('$this->actionPage?$this->firstFiled=$value')\" style=\"cursor: pointer; \" >$value</td>";    
                }
                else 
                {
                    echo "<td align=center bgcolor=white height=25>$value</td>";
                }        
            }
            echo "</tr>";
            $rs=@mysql_fetch_array($res,1);
        }    
        echo "</table>";
        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
        <iframe src="about:blank" name="Command" id="Command" style="display:none"></iframe>
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.php
复制PHP内容到剪贴板
PHP代码:
<script language=javascript src=include/order.js></script>
<?php
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内容到剪贴板
PHP代码:

<?php
include_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 "<script language=javascript>";
    echo "alert('Success!');";
    echo "top.location.reload();";
    echo "</script>";
}
else 
{
    echo "<script language=javascript>";
    echo "alert('Failed!');";
    echo "</script>";
}

?>

4.order.js
复制PHP内容到剪贴板
PHP代码:
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();
}

[ 本帖最后由 dongxin1390008 于 2007-7-15 19:55 编辑 ]

作者: dongxin1390008   发布时间: 2007-07-14

不错,慢慢研究

作者: sanler   发布时间: 2007-07-15

如果你想输出指定的列,那么就将sql语句提出来封装一下

作者: dongxin1390008   发布时间: 2007-07-15