Login   Register  
PHP Classes
elePHPant
Icontem

File: html_table_distri.php3

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of trigas  >  html_table  >  html_table_distri.php3  >  Download  
File: html_table_distri.php3
Role: ???
Content type: text/plain
Description: OO interface for HTML table generation
Class: html_table
Author: By
Last change:
Date: 2000-05-23 15:51
Size: 9,686 bytes
 

Contents

Class file image Download
<?

/*
  Author  : rouvas@di.uoa.gr
  Date    : 17/3/2000, 2/5/2000, 12/5/2000
  Vesrion : 0.0.5
  Description:
  A bundle of three classes to assist in outputing HTML TABLE.
  The table is defined row-wise with the granularity set at each cell.
  The idea behind it is that you first define the cells for a row.
  Then you add the cells to the row and start defining the cells for the next row.
  When you are done, you add the rows to the table.
  All the normal HTML tags can be defined at each level, that is cell, row or table.
  Each cell can be a different object, provided it implements the toString() method.
  If an object is found, then the toString() method of this object is called and a string
  representing it is expected to be returned. This string is embeded in the final output.
  The html_table->toString() method, returns the whole table as a string,
  whereas the html_table->show() method, echoes the table after constructing the relevant string.
  With the html_table->toString() method, you can get the whole table as a string and then
  use the string to mail the table or do further processing.
  Previous versions echoed directly the table to stdout whereas this version, the whole table
  is first constructed as a string and then echoed (html_tanle->show().
  This may cause problems in very large tables. The largest table that can be displayed with
  this method, depends on the maximum length of a PHP string.

  example:

  $c11 = new html_table_cell; $c11->add_cell('11');
  $c12 = new html_table_cell; $c12->add_cell('12');
  $c13 = new html_table_cell; $c13->add_cell('13');
  $c14 = new html_table_cell; $c14->add_cell('14');
  $r1 = new html_table_row;  $r1->add($c11); $r1->add($c12); $r1->add($c13); $r1->add($c14);
  $c21 = new html_table_cell; $c21->add_cell('21');
  $c22 = new html_table_cell; $c22->add_cell('22');
  $c23 = new html_table_cell; $c23->add_cell('23');
  $c24 = new html_table_cell; $c24->add_cell('24');
  $r2 = new html_table_row;  $r2->add($c22); $r2->add($c22); $r2->add($c23); $r2->add($c24);
  $t = new html_table;
  $t->add_row($r1); $t->add_row($r2);
  $t->show();
  
  or you can reuse variables, as in :
  
  $t = new html_table;
  $r = new html_table_row;
  $c = new html_table_cell; $c->add_cell('11'); $r->add($c);
  $c = new html_table_cell; $c->add_cell('12'); $r->add($c);
  $c = new html_table_cell; $c->add_cell('13'); $r->add($c);
  $c = new html_table_cell; $c->add_cell('14'); $r->add($c);
  $t->add_row($r);
  $r = new html_table_row;
  $c = new html_table_cell; $c->add_cell('21'); $r->add($c);
  $c = new html_table_cell; $c->add_cell('22'); $r->add($c);
  $c = new html_table_cell; $c->add_cell('23'); $r->add($c);
  $c = new html_table_cell; $c->add_cell('24'); $r->add($c);
  $t->add_row($r);
  $t->show();
  
  HTML headers, are provided for with the html_table_cell->add_header() call.
  example:
  $t = new html_table;
  $r = new html_table_row;
  $c = new html_table_cell; $c->add_header('11'); $r->add($c);
  $c = new html_table_cell; $c->add_header('12'); $r->add($c);
  $c = new html_table_cell; $c->add_header('13'); $r->add($c);
  $c = new html_table_cell; $c->add_header('14'); $r->add($c);
  $t->add_row($r);
  $r = new html_table_row;
  $c = new html_table_cell; $c->add_cell('21'); $r->add($c);
  $c = new html_table_cell; $c->add_cell('22'); $r->add($c);
  $c = new html_table_cell; $c->add_cell('23'); $r->add($c);
  $c = new html_table_cell; $c->add_cell('24'); $r->add($c);
  $t->add_row($r);
  $t->show();
  
  ChangeLog
  ~~~~~~~~~
  
  21/05/2000 : v.0.0.4, v.0.0.5, bug fixes, nested calls are done with show() or toString()
                                 depending on call, some memory optimizations as well
                                 If you have many nested tables, the memory usage decreases dramatically
                                 if you convert them to strings using the toString() method and dispose
                                 of the objects

  02/05/2000 : v.0.0.3, added the toSTring() method, useful for mailing the table
                        Changed the nested calls from
                        the show() method to toString() method
  
  17/03/2000 : v.0.0.1, v.0.0.2, original idea and implementation
  

*/

class html_table_cell {
  var $data;
  var $is_header;
  var $colspan;
  var $align;
  var $valign;
  var $bgcolor;
  var $rowspan;
  var $height;
  var $width;
  var $nowrap;
  var $class;
  var $style;
  var $str = '';
  var $show = FALSE;
  var $toString = FALSE;
  
  function output($o) {
    if ($this->show)
      if (is_object($o)) $o->show(); else echo $o;
    else
      if (is_object($o)) $this->str .= $o->toString(); else $this->str .= $o;
  }
  function add_cell($d) {
    $this->data = $d;
    $this->is_header = FALSE;
  }
  function add_header($d) {
    $this->data = $d;
    $this->is_header = TRUE;
  }
  function show() {
    $this->str = '';
    $this->show = TRUE; $this->toString = FALSE;
    $this->show_cell();
  }
  function show_cell() {
    if ($this->is_header) $this->output('<th'); else $this->output('<td');
    if (strlen(trim(strval($this->colspan)))!=0)   $this->output(' colspan="'.$this->colspan.'"');
    if (strlen(trim(strval($this->align)))!=0)     $this->output(' align="'.$this->align.'"');
    if (strlen(trim(strval($this->valign)))!=0)    $this->output(' valign="'.$this->valign.'"');
    if (strlen(trim(strval($this->bgcolor)))!=0)   $this->output(' bgcolor="'.$this->bgcolor.'"');
    if (strlen(trim(strval($this->rowspan)))!=0)   $this->output(' rowspan="'.$this->rowspan.'"');
    if (strlen(trim(strval($this->height)))!=0)    $this->output(' height="'.$this->height.'"');
    if (strlen(trim(strval($this->width)))!=0)     $this->output(' width="'.$this->width.'"');
    if (strlen(trim(strval($this->nowrap)))!=0)    $this->output(' nowrap="'.$this->nowrap.'"');
    if (strlen(trim(strval($this->class)))!=0)     $this->output(' class="'.$this->class.'"');
    if (strlen(trim(strval($this->style)))!=0)     $this->output(' style="'.$this->style.'"');
    $this->output('>');
    $this->output($this->data);
    if ($this->is_header) $this->output('</th>'."\n"); else $this->output('</td>'."\n");
  }
  function toString() {
    $this->str = '';
    $this->show = FALSE; $this->toString = TRUE;
    $this->show_cell();
    return $this->str;
  }
}

class html_table_row {
  var $data;
  var $align;
  var $valign;
  var $bgcolor;
  var $str = '';
  var $show = FALSE;
  var $toString = FALSE;
  
  function output($o) {
    if ($this->show)
      if (is_object($o)) $o->show(); else echo $o;
    else
      if (is_object($o)) $this->str .= $o->toString(); else $this->str .= $o;
  }
  function add($cell) {
    $this->data[] = $cell;
  }
  function show() {
    $this->str = '';
    $this->show = TRUE; $this->toString = FALSE;
    $this->show_row();
  }
  function show_row() {
    $this->output('<tr');
    if (strlen(trim(strval($this->align)))!=0)   $this->output(' align="'.$this->align.'"');
    if (strlen(trim(strval($this->valign)))!=0)  $this->output(' valign="'.$this->valign.'"');
    if (strlen(trim(strval($this->bgcolor)))!=0) $this->output(' bgcolor="'.$this->bgcolor.'"');
    $this->output('>');
    for ($i=0;$i<sizeof($this->data);$i++) $this->output($this->data[$i]);
    $this->output('</tr>');
  }
  function toString() {
    $this->str = '';
    $this->show = FALSE; $this->toString = TRUE;
    $this->show_row();
    return $this->str;
  }
}

class html_table {
  var $data;
  var $border;
  var $cellpadding;
  var $cellspacing;
  var $width;
  var $align;
  var $bgcolor;
  var $height;
  var $cols;
  var $hspace;
  var $vspace;
  var $str = '';
  var $show = FALSE;
  var $toString = FALSE;
  
  function output($o) {
    if ($this->show)
      if (is_object($o)) $o->show(); else echo $o;
    else
      if (is_object($o)) $this->str .= $o->toString(); else $this->str .= $o;
  }
  function add_row($r) {
    $this->data[] = $r;
  }
  function show() {
    $this->str = '';
    $this->show = TRUE; $this->toString = FALSE;
    $this->show_table();
  }
  function show_table() {
    $this->output('<table');
    if (strlen(trim(strval($this->border)))!=0)      $this->output(' border="'.$this->border.'"');
    if (strlen(trim(strval($this->cellspacing)))!=0) $this->output(' cellspacing="'.$this->cellspacing.'"');
    if (strlen(trim(strval($this->cellpadding)))!=0) $this->output(' cellpadding="'.$this->cellpadding.'"');
    if (strlen(trim(strval($this->width)))!=0)       $this->output(' width="'.$this->width.'"');
    if (strlen(trim(strval($this->align)))!=0)       $this->output(' align="'.$this->align.'"');
    if (strlen(trim(strval($this->bgcolor)))!=0)     $this->output(' bgcolor="'.$this->bgcolor.'"');
    if (strlen(trim(strval($this->height)))!=0)      $this->output(' height="'.$this->height.'"');
    if (strlen(trim(strval($this->cols)))!=0)        $this->output(' cols="'.$this->cols.'"');
    if (strlen(trim(strval($this->hspace)))!=0)      $this->output(' hspace="'.$this->hspace.'"');
    if (strlen(trim(strval($this->vspace)))!=0)      $this->output(' vspace="'.$this->vspace.'"');
    $this->output('>');
    for ($i=0;$i<sizeof($this->data);$i++) $this->output($this->data[$i]);
    $this->output('</table>');
  }
  function toString() {
    $this->str = '';
    $this->show = FALSE; $this->toString = TRUE;
    $this->show_table();
    return $this->str;
  }
}  

?>