tabel-class-v2
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
$settings = array('id'=>'test',
'class'=>'test_class',
'bgcolor'=>'#fff',
'border'=>1,
'cellpadding'=>0,
'width'=>'100%');
$oTable = new Table($settings);
$oTable->setRowClasses('odd','even','derde');
$oTable->setHead('col 1','col 2 en 3 <<2>>','col 4');
$oTable->newRow('Rij 1, Col 1','Rij 1, Col 2 en 3 <<2>>','Rij 1, Col 4');
$oTable->newRow('Rij 2, Col 1','Rij 2, Col 2','Rij 2, Col 3','Rij 2, Col 4');
$oTable->newRow('Rij 3, Col 1','Rij 3, Col 2 <<3>>');
$oTable->newRow('Rij 4, Col 1','Rij 4, Col 2','Rij 4, Col 3','Rij 4, Col 4');
$oTable->newRow('Rij 5, Col 1 <<4>>');
$oTable->newRow('Rij 6, Col 1 <<3>>','Rij 4, Col 4');
$oTable->newRow('Rij 7, Col 1','Rij 7, Col 2','Rij 7, Col 3','Rij 7, Col 4');
?>
$settings = array('id'=>'test',
'class'=>'test_class',
'bgcolor'=>'#fff',
'border'=>1,
'cellpadding'=>0,
'width'=>'100%');
$oTable = new Table($settings);
$oTable->setRowClasses('odd','even','derde');
$oTable->setHead('col 1','col 2 en 3 <<2>>','col 4');
$oTable->newRow('Rij 1, Col 1','Rij 1, Col 2 en 3 <<2>>','Rij 1, Col 4');
$oTable->newRow('Rij 2, Col 1','Rij 2, Col 2','Rij 2, Col 3','Rij 2, Col 4');
$oTable->newRow('Rij 3, Col 1','Rij 3, Col 2 <<3>>');
$oTable->newRow('Rij 4, Col 1','Rij 4, Col 2','Rij 4, Col 3','Rij 4, Col 4');
$oTable->newRow('Rij 5, Col 1 <<4>>');
$oTable->newRow('Rij 6, Col 1 <<3>>','Rij 4, Col 4');
$oTable->newRow('Rij 7, Col 1','Rij 7, Col 2','Rij 7, Col 3','Rij 7, Col 4');
?>
Class:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
class Table{
private $settings = array();
private $rowsCount = 0;
private $colsCount = 0;
private $rows = array();
private $head = array();
private $rowClasses = array();
public function __construct($set){
$this->settings = $set;
}
private function updateColsCount($nr){
if($nr > $this->colsCount){
$this->colsCount = $nr;
}
}
private function updateRowsCount(){
$this->rowsCount++;
}
public function setHead(){
$args = func_get_args();
$this->head = $args;
$this->updateColsCount(count($args));
}
public function setRowClasses(){
$args = func_get_args();
$this->rowClasses = $args;
}
public function newRow(){
$args = func_get_args();
$this->rows[] = $args;
$this->updateColsCount(count($args));
$this->updateRowsCount();
}
public function checkStyles(){
$valid = array('id','class','bgcolor','border','cellpadding','width');
$return = '';
foreach($this->settings as $key => $value){
if(in_array($key, $valid)){
$return .= ' '.$key.'="'.$value.'"';
}
}
return $return;
}
public function display(){
$return = '';
if(!empty($this->rowsCount)){
$return .= '<table'.$this->checkStyles().'>';
if(!empty($this->head)){
$return .= '<tr>';
$s = 0;
for($i=0;$i<$this->colsCount; $i++){
if(isset($this->head[$i])){
preg_match('/([-a-zA-Z0-9_,;:#!=*?\'+\%.$\%() ]{0,}) (<<([0-9]{1,3})>>)/', $this->head[$i], $thisrow);
if($thisrow){
$return .= '<th colspan="'.$thisrow[3].'">'.$thisrow[1].'</th>';
$s += $thisrow[3];
}else{
$return .= '<th>'.$this->head[$i].'</th>';
$s++;
}
}else{
$return .= '<th> </th>';
$s++;
}
if($s >= $this->colsCount){
break;
}
}
$return .= '</tr>';
}
for($a=0;$a<$this->rowsCount;$a++){
if(isset($this->rowClasses)){
$class = ' class="'.$this->rowClasses[$a%count($this->rowClasses)].'"';
}else{
$class = '';
}
$return .= '<tr'.$class.'>';
$s = 0;
for($b=0;$b<$this->colsCount; $b++){
if(isset($this->rows[$a][$b]) AND $this->rows[$a][$b] != null AND $this->rows[$a][$b] != ''){
preg_match('/([-a-zA-Z0-9_,;:#!=*?\'+\%.$\%() ]{0,}) (<<([0-9]{1,3})>>)/', $this->rows[$a][$b], $thisrow);
if($thisrow){
$return .= '<td colspan="'.$thisrow[3].'">'.$thisrow[1].'</td>';
$s += $thisrow[3];
}else{
$return .= '<td>'.$this->rows[$a][$b].'</td>';
$s++;
}
}else{
$return .= '<td> </td>';
$s++;
}
if($s >= $this->colsCount){
break;
}
}
$return .= '</tr>';
}
$return .= '</table>';
}else{
$return .= 'Geen geldig aantal rijen';
}
return $return;
}
}
?>
class Table{
private $settings = array();
private $rowsCount = 0;
private $colsCount = 0;
private $rows = array();
private $head = array();
private $rowClasses = array();
public function __construct($set){
$this->settings = $set;
}
private function updateColsCount($nr){
if($nr > $this->colsCount){
$this->colsCount = $nr;
}
}
private function updateRowsCount(){
$this->rowsCount++;
}
public function setHead(){
$args = func_get_args();
$this->head = $args;
$this->updateColsCount(count($args));
}
public function setRowClasses(){
$args = func_get_args();
$this->rowClasses = $args;
}
public function newRow(){
$args = func_get_args();
$this->rows[] = $args;
$this->updateColsCount(count($args));
$this->updateRowsCount();
}
public function checkStyles(){
$valid = array('id','class','bgcolor','border','cellpadding','width');
$return = '';
foreach($this->settings as $key => $value){
if(in_array($key, $valid)){
$return .= ' '.$key.'="'.$value.'"';
}
}
return $return;
}
public function display(){
$return = '';
if(!empty($this->rowsCount)){
$return .= '<table'.$this->checkStyles().'>';
if(!empty($this->head)){
$return .= '<tr>';
$s = 0;
for($i=0;$i<$this->colsCount; $i++){
if(isset($this->head[$i])){
preg_match('/([-a-zA-Z0-9_,;:#!=*?\'+\%.$\%() ]{0,}) (<<([0-9]{1,3})>>)/', $this->head[$i], $thisrow);
if($thisrow){
$return .= '<th colspan="'.$thisrow[3].'">'.$thisrow[1].'</th>';
$s += $thisrow[3];
}else{
$return .= '<th>'.$this->head[$i].'</th>';
$s++;
}
}else{
$return .= '<th> </th>';
$s++;
}
if($s >= $this->colsCount){
break;
}
}
$return .= '</tr>';
}
for($a=0;$a<$this->rowsCount;$a++){
if(isset($this->rowClasses)){
$class = ' class="'.$this->rowClasses[$a%count($this->rowClasses)].'"';
}else{
$class = '';
}
$return .= '<tr'.$class.'>';
$s = 0;
for($b=0;$b<$this->colsCount; $b++){
if(isset($this->rows[$a][$b]) AND $this->rows[$a][$b] != null AND $this->rows[$a][$b] != ''){
preg_match('/([-a-zA-Z0-9_,;:#!=*?\'+\%.$\%() ]{0,}) (<<([0-9]{1,3})>>)/', $this->rows[$a][$b], $thisrow);
if($thisrow){
$return .= '<td colspan="'.$thisrow[3].'">'.$thisrow[1].'</td>';
$s += $thisrow[3];
}else{
$return .= '<td>'.$this->rows[$a][$b].'</td>';
$s++;
}
}else{
$return .= '<td> </td>';
$s++;
}
if($s >= $this->colsCount){
break;
}
}
$return .= '</tr>';
}
$return .= '</table>';
}else{
$return .= 'Geen geldig aantal rijen';
}
return $return;
}
}
?>