Scripts
Tabel Class [v2]
Met deze class is het mogelijk, snel en simpel of juist heel uitgebreide tabellen te maken. Versies: Oude versie: http://phphulp.nl/php/scripts/4/1736/ Gebruik: Rowspans: Door: " " toe tevoegen achter het veld. Waar NR het aantal kollommen is van de rowspan. Wordt er een Rowspan van dat veld gemaakt. De andere velden worden automatisch weggehaald, ook als ze inhoud bevatten!
tabel-class-v2
[code]
<?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');
?>
[/code]
[b]Class:[/b]
[code]
<?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;
}
}
?>
[/code]
Reacties
0