Scripts
Tabel Class [v3]
Met deze class (in oop :']) kan je snel en gemakkelijk uitgebreide tabellen maken. De structuur is als volgt: Tabel » Heeft alle rijen in zich. Bevat de settings en eigenschappen van de tabel. Genereert de tabel; en alternate de rij kleur. Row » Heeft alle cellen in zich die in die row thuis horen. Heeft een ook een variable die zegt of de rij een header is of niet. Cell » Heeft zelf eigenschappen voor de cel (colspan, htmlentities) Voor uitgebreide uitleg check de tutorial van Blance: http://phphulp.nl/php/tutorials/8/701/1845/ Versies: v1: http://phphulp.nl/php/scripts/4/1736/ v2: http://phphulp.nl/php/scripts/4/1739/
tabel-class-v3
[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->autoAanvullen = false;
$row = new Row(true);
$row->addCell(new Cell('Header, Col 1'));
$row->addCell(new Cell('Header, Col 2'));
$oTable->addRow($row);
$row = new Row;
$row->addCell(new Cell('Rij 11 <b>test</b>, Col 1'));
$row->addCell(new Cell('Rij 11 <b>test</b>, Col 2 en 3',2,false));
$oTable->addRow($row);
for($i=1;$i<11;$i++){
$row = new Row;
$row->addCell(new Cell('Rij '.$i.', Col 1'));
$row->addCell(new Cell('Rij '.$i.', Col 2'));
$row->addCell(new Cell('Rij '.$i.', Col 3'));
$row->addCell(new Cell('Rij '.$i.', Col 4'));
$oTable->addRow($row);
}
$row = new Row;
$row->addCell(new Cell('Rij 11 <b>test</b>, Col 1 en 2',2));
$row->addCell(new Cell('Rij 11 <b>test</b>, Col 3'));
$row->addCell(new Cell('Rij 11 <b>test</b>, Col 4'));
$oTable->addRow($row);
?>
[/code]
[b]Class:[/b]
[code]
<?php
class Table{
private $settings = array();
private $cCount = 0;
private $rows = array();
private $rowClasses = array();
public $leegWaarde = ' ';
public $autoAanvullen = true;
public function __construct($set){
$this->settings = $set;
}
public function setRowClasses(){
$args = func_get_args();
$this->rowClasses = $args;
}
public function addRow(Row $row) {
$this->rows[] = $row;
$this->updateColsCount(count($row->cells));
}
private function updateColsCount($nr){
if($nr > $this->cCount){
$this->cCount = $nr;
}
}
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 = '';
$return .= '<table'.$this->checkStyles().'>';
for($i=0;$i<count($this->rows);$i++){
$class = (isset($this->rowClasses)) ? ' class="'.$this->rowClasses[$i%count($this->rowClasses)].'"': '';
$return .= '<tr'.$class.'>';
$s = 0;
for($a = 0;$a < $this->cCount;$a++){
if(isset($this->rows[$i]->cells[$a])){
$ent = $this->rows[$i]->cells[$a]->htmlenteties;
$cont = ((isset($this->rows[$i]->cells[$a]->content)) ? (($ent) ? htmlentities($this->rows[$i]->cells[$a]->content): $this->rows[$i]->cells[$a]->content): $this->leegWaarde);
$return .= '<'.(($this->rows[$i]->rHead) ? "th": "td").' colspan="'.$this->rows[$i]->cells[$a]->colspan.'">'.$cont.'<'.(($this->rows[$i]->rHead) ? "/th": "/td").'>';
$s += $this->rows[$i]->cells[$a]->colspan;
}elseif(!isset($this->rows[$i]->cells[$a]) && $s<$this->cCount AND $this->autoAanvullen){
$return .= '<'.(($this->rows[$i]->rHead) ? "th": "td").'>'.$this->leegWaarde.'<'.(($this->rows[$i]->rHead) ? "/th": "/td").'>';
$s++;
}
}
$return .= '<tr/>';
}
$return .= '</table>';
return $return;
}
public function debugGetRows(){
return print_r($this->rows);
}
}
class Row{
public $cells;
public function __construct($header = false) {
$this->rHead = $header;
}
public function addCell(Cell $cell) {
$this->cells[] = $cell;
}
public function debugGetCells(){
return print_r($this->cells);
}
}
class Cell{
public $colspan;
public $content;
public $htmlenteties;
public function __construct($content, $span = 1, $htmlenteties = false) {
$this->content = $content;
$this->colspan = $span;
$this->htmlenteties = $htmlenteties;
}
}
?>
[/code]
Reacties
0