用php实现的数据网格类

来源:互联网 发布:休闲零食市场数据 编辑:程序博客网 时间:2024/05/01 23:48

 

php实现的数据网格类
文件class.DataGrid.php
<?php
/******************************************************
*版权:高西林
*版本:1.1
*文件名:class.DataGrid.php
*作者:高西林
*日期:2007-07-30
*描述:数据网格,给datagrid绑定的所有行的键值不能有
       相同的。
*修改记录:
*修改人    日期      修改记录
 高西林 2007-07-31 修正rows没有关键字时无法生成表格的缺陷。
                    将版本从1.0升至1.1。 
*******************************************************/
class DataGrid{
       public $rows = array();
       public $columns = array();
      
       public $gridName = 'DataGrid';//网格名称
       public $caption = 'DatagGrid';//网格标题
       public $footer = '';//网格footer
      
       public $styleClass = ''; //样式类
      
       public $isAutoColumn = 1;//是否自动生成列名
       public $hasCaption = 0;//网格是否有标题
       public $hasHeader = 1;//网格是否有表头
       public $hasFooter = 1;//是否有footer
       public $hasRowHeader = 1;//是否有行表头
      
      
       //
       public function __construct(){
       }
       public function __destruct(){
       }
      
       //
       public function createGridHtml(){
              if(count($this -> rows) <= 0)return;
             
              $gridhtml = '<table class="' . $this -> styleClass . '">';
             
              if($this -> hasCaption)$gridhtml .= $this -> gridCaption();
             
              if($this -> hasHeader) $gridhtml .= $this -> gridHeader();
             
              $gridhtml .= $this -> gridBody();
             
              if($this -> hasFooter) $gridhtml .= $this -> gridFooter();
             
              $gridhtml .= '</table>';
             
              return $gridhtml;
       }
      
       //
       private function gridCaption(){
              return '<caption>' . $this -> caption . '</caption>';
       }
      
       //
       private function gridHeader(){
              $header = '<thead><tr>';
             
              if($this -> hasRowHeader)$header .= '<th></th>';
              if($this -> isAutoColumn){
                     $current = current($this -> rows);
               $columns = array_keys($current);
                     for($i = 0; $i < count($columns) ; $i++){
                            $header .= '<th>' . $columns[$i] . '</th>';
                     }
              }else{
                     $columns = array_values($this -> columns);
                     for($i = 0; $i < count($columns) ; $i++){
                            $header .= '<th>' . $columns[$i] . '</th>';
                     }
              }
    $header .= "</tr></thead>";
    return $header;
       }
       private function gridBody(){
              $body = '<tbody>';
              $i = 0 ;
              reset($this -> rows);
              while($row = current($this -> rows)){
                     $body .= '<tr>';
                     $key = key($this -> rows);
                     if($this -> hasRowHeader)$body .= '<th>' . $key . '</th>';
                     $row = $this -> rows[$key];
                     if($this -> isAutoColumn){
                            while($rowkey = key($row)){
                                          $body .= '<td>' . $row[$rowkey] . '</td>';
                                          next($row);
                            }
                     }else{
                            reset($this -> columns);
                            while($rowkey = key($this -> columns)){
                                          $body .= '<td>' . $row[$rowkey] . '</td>';
                                          next($this -> columns);
                            }
                     }
                     $body .= '</tr>';
                     $i += 1;
                     next($this -> rows);
              }
              $body .= '</tbody>';
              return $body;
       }
       private function gridFooter(){
              return '<tfoot>' . $this -> footer . '</tfoot>';
       }
}
?>
测试文件
<?php
/**********************************************
*版权:高西林
*文件名:loglist.php
*作者:高西林
*日期:2007-07-30
*描述:日志显示
*修改记录:
***********************************************/
 
include('./ class.DataGrid.php');
include('./ class.PageBar.php');
 
$data = array('HTML 4.01' => array('MSIE 6.0' => '1' , 'Firefox 1.0' => '2' , 'Firefox 1.5' => '3' , 'Opera 8.5' => '4'),
              'dfsa' => array('MSIE 6.0' => '5' , 'Firefox 1.0' => '6' , 'Firefox 1.5' => '7' , 'Opera 8.5' => '8'),
              'dddd' => array('MSIE 6.0' => '9' , 'Firefox 1.0' => '10' , 'Firefox 1.5' => '11' , 'Opera 8.5' => '12'),
              'aaaa' => array('MSIE 6.0' => '13' , 'Firefox 1.0' => '14' , 'Firefox 1.5' => '15' , 'Opera 8.5' => '16'),
              'dfsd' => array('MSIE 6.0' => '13' , 'Firefox 1.0' => '14' , 'Firefox 1.5' => '15' , 'Opera 8.5' => '16'),
              'dfdsfds' => array('MSIE 6.0' => '13' , 'Firefox 1.0' => '14' , 'Firefox 1.5' => '15' , 'Opera 8.5' => '16'),
              '哈哈哈' => array('MSIE 6.0' => '13' , 'Firefox 1.0' => '14' , 'Firefox 1.5' => '15' , 'Opera 8.5' => '16'),
);
 
$datagrid = new DataGrid();
$datagrid -> rows = $data;
$datagrid -> styleClass = 'gridstyle';
$datagrid -> hasRowHeader = 1;
 
$pageBar = new PageBar();
$pageBar -> pageindex = $pageindex;
$pageBar -> total = 20;
$pageBar -> perpage = 5;
$pageBar -> joinword = '?';
$pageBar -> pageindexword = 'pageindex';
$pageBar -> navigateurl = 'loglist.php';
$pageBar -> pagebarstyle = 4;
$pageBar -> pagenoperpage = 5;
$navigatebar = $pageBar -> createPageBar();
 
$datagrid -> footer = ' <tr><td colspan="5">' . $navigatebar . '</td></tr>';
$gridhtml = $datagrid -> createGridHtml();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" rev="stylesheet" href="css/gridstyle.css" />
<title>日志查询</title>
</head>
 
<body>
 <div id="right">
          <?php echo $gridhtml;?>
       </div>
</body>
</html>
设置数据网格风格的css样式文件内容:
.gridstyle{
 width:700px;
 margin:0px auto;
 border-collapse:collapse;
 
 text-align:center;
}
.gridstyle th ,td{
 border:1px solid #aaa;
}
.gridstyle thead th{
 border-bottom:2px solid #2d580b;
 background-color:#8fc629;
 color:#fff;
 padding:10px 0px;
}
.gridstyle th{
 background-color:#f2f4b9;
}
.gridstyle th.title{
 background-color:#e3e685;
}
.gridstyle tfoot td{
 border-width:0px;
 text-align:right;
 font-size:12px;
 color:#777;
}
.gridstyle caption{
 font-weight:bold;
 padding:6px 0px;
 color:#3d580b;
 font-size:25px;
}
 
文件class.PageBar.php文件就是本blog中的那个分页类,将其拷贝到这个文件中即可。
 
 
 
 
 
 
 
最终效果:
 
MSIE 6.0
Firefox 1.0
Firefox 1.5
Opera 8.5
HTML 4.01
1
2
3
4
dfsa
5
6
7
8
dddd
9
10
11
12
aaaa
13
14
15
16
dfsd
13
14
15
16
dfdsfds
13
14
15
16
哈哈哈
13
14
15
16
[<<]  [<] [1] [2] [3] [4] [>][>>] 
 
原创粉丝点击