Navigation überspringen

OOP - Tabellen erzeugen

TableBuilder.php

/**
 * Description of TableBuilder
 * @author T.Smit, 2015
 */
class TableBuilder {

    public function tableBuilderOpen($anzahlCols, array $colwidth = Null, $tablewidth = Null, $tableheight = Null, $cssClass = 'normaltable') {
        if ($tablewidth) {
            $width = $tablewidth;
        } else {
            $width = '100%';
        }
        if ($tableheight) {
            $height = $tableheight;
        } else {
            $height = '100%';
        }
        $result = '<table border="0" width="' . $width . '" height="' . $height . '" cellpadding="0" cellspacing="0" class="' . $cssClass . '">';
        if ($colwidth) {
            $result.='<colgroup>';
            for ($i = 0; $i < $anzahlCols; $i++) {
                $result.='<col width="' . $colwidth[$i] . '">';
            }
            $result.='</colgroup>';
        }
        return $result;
    }

    public function tableNewRow(array $items, array $hAlignment, array $vAlignment, $anzahlCols, array $height = Null, array $cssID = Null) {
        if(!$cssID){
            $cssID=array_fill(0, $anzahlCols, 'main');
        }
        $result = '<tr>';
        for ($i = 0; $i < count($items); $i++) {
            $result.= '<td align="' . $hAlignment[$i] . '" valign="' . $vAlignment[$i] . '" height="' . $height[$i] . '">';
            $result.= '<div id='.$cssID[$i].' >';
            $result.= $items[$i];
            $result.='</div>';
            $result.= '</td>';
        }
        $i++;
        while ($i < $anzahlCols) {
            $result.= '<td></td>';
        }
        $result.= '</tr>';
        return $result;
    }

    public function tableNewRowMulticol($item, $anzahlCols, $hAlignment, $vAlignment, $height = Null, $cssID='main') {
        $result = '<tr>';
        $result.= '<td align="' . $hAlignment . '" valign="' . $vAlignment . '" height="' . $height . '" id="' . $cssID . '" colspan="'.$anzahlCols.'">';
        $result.= $item;
        $result.= '</td>';
        $result.= '</tr>';
        return $result;
    }

    public function tableBuilderClose() {
        $result = '</table>';
        return $result;
    }

}

Verwendung TableBuilder-Klasse

$ueberschriften = array('ID', 'Nachname','Vorname', 'KFZ', 'Email');
$anzahlSpalten = count($ueberschriften);
$horizOrientation = array();
$vertOrientation = array();
$tdHeader = array();
$tdOdd = array();
$tdEven = array();
for ($n = 0; $n < $anzahlSpalten; $n++) {
    $horizOrientation[] = 'left';
    $vertOrientation[] = 'top';
    //css Header-row
    $tdHeader[] = 'tdHeader';
    $tdOdd[] = 'odd';
    $tdEven[] = 'even';
}
echo $tableTopFrameMain->tableBuilderOpen($anzahlSpalten, array('10%', '20%', '20%', '20%', '30%'), '100%');
echo $tableTopFrameMain->tableNewRow($ueberschriften, $horizOrientation, $vertOrientation, $anzahlSpalten, NULL, $tdHeader);
//Datenbank-Objekt verwendet
$DBSQL->query("SELECT * FROM mitarbeiter ORDER BY MitarbeiterNachname;");
$i = 0;
while ($resultMitarbeiter = $DBSQL->fetchobject()) {
    $werteArray = array();
    $i++;
    if ($i % 2 == 0) {
        $cssID = $tdOdd;
    } else {
        $cssID = $tdEven;
    }
    $werteArray[]=$resultMitarbeiter->ID;
    $werteArray[]=$resultMitarbeiter->Nachname;
    $werteArray[]=$resultMitarbeiter->Vorname;
    $werteArray[]=$resultMitarbeiter->KFZ;
    $werteArray[]=$resultMitarbeiter->Email;
    echo $tableTopFrameMain->tableNewRow($werteArray, $horizOrientation, $vertOrientation, $anzahlSpalten, NULL, $cssID);
}
echo $tableTopFrameMain->tableBuilderClose();