Classe HTML table

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
43
Massa, Italy
Salve, volevo sapere si come che ho trovato una classe ad questo indirizzo:
http://www.dyn-web.com/code/table_class/

gli esempi sono questi:
html_table.class.php
PHP:
<?php

// html_table.class.php
// version date: dec 2008 

class HTML_Table {
    
    private $rows = array();
    private $tableStr = '';
    
    function __construct($id = NULL, $klass = NULL, $border = 0, $cellspacing = 2, $cellpadding = 0, $attr_ar = array() ) {
        $this->tableStr = "\n<table" . ( !empty($id)? " id=\"$id\"": '' ) . 
            ( !empty($klass)? " class=\"$klass\"": '' ) . $this->addAttribs( $attr_ar ) . 
             " border=\"$border\" cellspacing=\"$cellspacing\" cellpadding=\"$cellpadding\">\n";
    }
    
    private function addAttribs( $attr_ar ) {
        $str = '';
        foreach( $attr_ar as $key=>$val ) {
            $str .= " $key=\"$val\"";
        }
        return $str;
    }
    
    public function addRow($klass = NULL, $attr_ar = array() ) {
        $row = new HTML_TableRow( $klass, $attr_ar );
        array_push( $this->rows, $row );
    }
    
    public function addCell($data = '', $klass = NULL, $type = 'data', $attr_ar = array() ) {
        $cell = new HTML_TableCell( $data, $klass, $type, $attr_ar );
        // add new cell to current row's list of cells
        $curRow = &$this->rows[ count( $this->rows ) - 1 ]; // copy by reference
        array_push( $curRow->cells, $cell );
    }
    
    public function display() {
        foreach( $this->rows as $row ) {
            $this->tableStr .= !empty($row->klass) ? "  <tr class=\"$row->klass\"": "  <tr";
            $this->tableStr .= $this->addAttribs( $row->attr_ar ) . ">\n";
            $this->tableStr .= $this->getRowCells( $row->cells );
            $this->tableStr .= "  </tr>\n";
        }
        $this->tableStr .= "</table>\n";
        return $this->tableStr;
    }
    
    function getRowCells($cells) {
        $str = '';
        foreach( $cells as $cell ) {
            $tag = ($cell->type == 'data')? 'td': 'th';
            $str .= !empty($cell->klass) ? "    <$tag class=\"$cell->klass\"": "    <$tag";
            $str .= $this->addAttribs( $cell->attr_ar ) . ">";
            $str .= $cell->data;
            $str .= "</$tag>\n";
        }
        return $str;
    }
    
}


class HTML_TableRow {
    function __construct($klass = NULL, $attr_ar = array()) {
        $this->klass = $klass;
        $this->attr_ar = $attr_ar;
        $this->cells = array();
    }
}

class HTML_TableCell {
    function __construct( $data, $klass, $type, $attr_ar ) {
        $this->data = $data;
        $this->klass = $klass;
        $this->type = $type;
        $this->attr_ar = $attr_ar;
    }
}

?>

e l'esempio è questo:
example
PHP:
<?php
require_once(dirname(__FILE__) . '/html_table.class.php');

$PRODUCTS = array(
    'choc_chip' => array(' Chocolate Chip Cookies', 1.25, 10.00),
    'oatmeal' => array('Oatmeal Cookies', .99, 8.25),
    'brwwnies' => array('Fudge Brownies', 1.35, 12.00)
);

$tbl = new HTML_Table(null, 'display', 1, 0, 4);

$tbl->addRow();
    $tbl->addCell('Product', 'first', 'header');
    $tbl->addCell('Single Item Price', null, 'header');
    $tbl->addCell('Price per Dozen', null, 'header');
  
    foreach($PRODUCTS as $product) {
        list($name, $unit_price, $doz_price ) = $product;
        $tbl->addRow();
            $tbl->addCell($name);
            $tbl->addCell('$' . number_format($unit_price, 2), 'num' );
            $tbl->addCell('$' . number_format($doz_price, 2), 'num' );
			
    }
echo $tbl->display();
?>

io vorrei fare questa tabella:
Codice:
   <table width="100%" border="0">
  <tr>
    <td width="91%"><h2><?php echo $strTitolo; ?></h2></td>
    <td width="9%"><?php echo "Pubblicato:&nbsp;".$dtmPubblicazione."&nbsp;"; ?></td>
  </tr>
  <tr>
    <td colspan="2" align="left" valign="top"><?php echo fInterpreta($strTesto).""; ?></td>
  </tr>
 
</table>

come posso fare con la classe??

Non mi dite di usare i div e i css .. perché non li capisco ancora molto e mi ci vuole di più per fare una riga con i css e i div..

ma volevo usare questa classe..

mi dite come posso fare?

grazie mille.
 

Discussioni simili