Class de tableau html

Soyez le premier à donner votre avis sur cette source.

Vue 9 926 fois - Téléchargée 714 fois

Description

Cette classe permet de créer des objets tableau (<TABLE>) HTML, de le remplir avec un tableau de chaine de caractère et de l'afficher.

Source / Exemple :


<?php
/**

  • Project:
  • File:
  • Class: Element, Cell, Row, Table
*
  • This class makes you able to create an HTML array(<table>), to fill it in
  • with data arrays and to display it. This class can used in association with
  • CSS.
*
  • @author Jean Poldeux <jean_poldeux@hotmail.com>
  • @date July 29th, 2005
  • @package fmksite
  • @version 0.1
  • /
/**
  • This class is used to create any table element.
  • /
class Element { /**
  • Constructor that creates a table object with all the xhtml commun attributes
  • to all xhtml tags that compose a table (<table>, <th>, <td>, <tr>).
  • @param name Element name. It'll be used as an ID if you want to use javascript (STRING)
  • @param background_color Background color of this element. Use a name or an Hexadecimal value (STRING)
  • @param alignment This parameter represents the alignement (table and/or text) (INTEGER)
  • and can take these values :
  • <ul>
  • <li> 1 => left (Default Value)</li>
  • <li> 2 => center </li>
  • <li> 3 => right </li>
  • <ul>
  • /
function Element($name = "Element", $background_color = "white", $alignment = 1) { $this->att["name"] = $name; $this->att["bgcolor"] = $background_color; switch($alignment) { case:2 $this->att["align"] = "center"; break; case:3 $this->att["align"] = "right"; break; default $this->att["align"] = "left"; break; } } /**
  • Set the alignment of the table element
  • @param alignment This parameter represents the alignement (table and/or text) (INTEGER)
  • and can take these values :
  • <ul>
  • <li> 1 => left (Default Value)</li>
  • <li> 2 => center </li>
  • <li> 3 => right </li>
  • <ul>
  • /
function setAlignment($alignment = 1) { switch($alignment) { case:2 $this->att["align"] = "center"; break; case:3 $this->att["align"] = "right"; break; default $this->att["align"] = "left"; break; } } /**
  • Set the background color of the element xhtml tag
  • @param background_color Background color of this element. Use a name or an Hexadecimal value (STRING)
  • /
function setBackgroundColor($background_color = "white") { $this->att["bgcolor"] = $background_color; } /**
  • Set the class attribute of the element xhtml tag
  • @param class The value of class attribute - default="" (STRING)
  • /
function setClass($class = "") { $this->att["class"] = $class; } /**
  • Set the id attribute of the element xhtml tag
  • @param id The value of id attribute - default="" (STRING)
  • /
function setId($id = "") { $this->att["id"] = $id; } /**
  • Set the style attribute of the element xhtml tag. Use this if you want to use
  • a special font or foreground color.
  • @param style The value of style attribute - default="" (STRING)
  • /
function setStyle($style = "") { $this->att["style"] = $style; } /**
  • Add a new attribute of the component xhtml tag
  • @param name The name of the new attribute (STRING)
  • @param value The value of this attribute - default = "" (STRING)
  • @return (BOOLEAN) TRUE if the new attribute has correctly been added - FALSE if it already exits or an error occured
  • /
function addAtt($name, $value = "") { if(!array_key_exists($name, $this->att) && strlen($value) != 0 ) { $this->att[$name] = $value; return true; } else return false; } /**
  • Set a new value to xhtml attribute given as parameter
  • @param attribute The name of the attribute (STRING)
  • @param value The value of this attribute - default = "" (STRING)
  • @return (BOOLEAN) TRUE if the attribute has correctly been changed - FALSE if it doesn't exits or an error occured
  • /
function setAtt($name, $value = "") { if(array_key_exists($name, $this->att) && strlen($value) != 0 ) { $this->att[$name] = $value; return true; } else return false; } /**
  • Remove a xhtml attribute of this component
  • @param attribute The name of the attribute (STRING)
  • @return (BOOLEAN) TRUE if the attribute has correctly been removed - FALSE if it doesn't exits or an error occured
  • /
function removeAtt($name) { if(array_key_exists($name, $this->att)) { //Find the place of this element $index = array_search($name, array_keys($this->att)); //remove the attribute array_splice($this->att, $index, 1); //return true return true; } else return false; } /**
  • This function makes you able to display the element attribute within a xhtml tag
  • /
function display() { //Initialise the display string var $var = ""; //Point to the first element of the attribute array reset($this->att); //Loop through the array and extract the key and value while(list($attribute, $value) = each($this->att)) { //check if the attribute is not empty if(isset($value) && strlen($value) != 0) { $var = $attribute."=\"".$value."\" "; } } //display the obtained string echo $var; } /**
  • @var att An indexed array that contains the xhtml attribute as key and
  • the xhtml value as value
  • /
var $att = array(); } /**
  • This class can be used to create a cell within a row object.
  • /
class Cell extends Element { /**
  • Constructor that initialises the cell dimension and the common xhtml attributes
  • @param name The cell name. It'll be used as an ID if you want to use javascript (STRING)
  • @param value The cell value which will be displayed in it (STRING)
  • @param background_color Background color of this Cell. Use a name or an Hexadecimal value (STRING)
  • @param horizontal_alignment This parameter represents the text alignement (INTEGER)
  • and can take these values :
  • <ul>
  • <li> 1 => left (Default Value)</li>
  • <li> 2 => center </li>
  • <li> 3 => right </li>
  • <ul>
  • @param height The cell height given in pixel or percent (e.g. "75px" or "20%") (STRING)
  • @param width The cell width given in pixel or percent (e.g. "75px" or "20%") (STRING)
  • /
function Cell($name = "Cell", $value = "&nbsp;", $background_color = "white", $horizontal_alignment = 1, $height = "75px", $width = "75px") { //Call the super constructor $this->Element($name, $background_color, $horizontal_alignment); //Initialise the own attributes $this->att["height"] = $height; $this->att["width"] = $width; //Initilise the value $this->value = $value; } /**
  • Set the value that will be displayed in the cell
  • @param value The cell value which will be displayed in it (STRING)
  • /
function setValue($value = "&nbsp;") { $this->value = $value; } /**
  • Set the cell height
  • @param height The cell height given in pixel or percent (e.g. "75px" or "20%") (STRING)
  • /
function setHeight($height = "75px") { $this->att["height"] = $height; } /**
  • Set the cell width
  • @param width The cell width given in pixel or percent (e.g. "75px" or "20%") (STRING)
  • /
function setWidth($width = "75px") { $this->att["width"] = $width; } /**
  • Set the text vertical alignement.
  • @param vertical_alignment This parameter represents the text alignement (INTEGER)
  • and can take these values :
  • <ul>
  • <li> 1 => up </li>
  • <li> 2 => center (Default value)</li>
  • <li> 3 => bottom </li>
  • <li> 4 => baseline (It follows text line) </li>
  • <ul>
  • /
function setVerticalAlignement($vertical_alignment = 2) { switch($vertical_alignment) { case:1 $this->att["valign"] = "top"; break; case:3 $this->att["valign"] = "bottom"; break; case:4 $this->att["valign"] = "baseline"; break; default $this->att["valign"] = "center"; break; } } /**
  • Enable or Disable Carriage return within a cell
  • @param new_line True if to enable it - False to disable. (BOOLEAN)
  • /
function setNewLine($new_line = true) { $this->new_line = $new_line; } /**
  • Set the number of rows used by this cell
  • @param rows The number of rows (INTEGER).
  • /
function setRows($rows = 1) { $this->att["rowspan"] = $rows; } /**
  • Set the number of Columns used by this cell
  • @param cols The number of columns (INTEGER).
  • /
function setColumns($cols = 1) { $this->att["colspan"] = $cols; } /**
  • Set if the cell is a column title or not. The difference is that the text is
  • automaticaly bold and centered or not.
  • @param $tilte True if it's a title - False if not (BOOLEAN).
  • /
function setTitle($title = false) { $this->title = $title; } /**
  • Display this cell within a table
  • /
function display() { //Display the open tag if($this->title) { echo "<th "; } else { echo "<td "; } Element::display(); echo ">\n"; //Display the value echo $value."\n"; //Close the tag echo "</td>\n"; } /**
  • @var value The cell value (STRING)
  • /
var $value = "&nbsp;"; /**
  • @var Title True if the cell if column title - false if not (BOOLEAN)
  • /
var $title = false; /**
  • @var new_line Carriage return within a cell (BOOLEAN)
  • /
var $new_line = true; } /**
  • This class is used to create a row within a table object. You can add as many
  • Cell objects as you want.
  • /
class Row extends Element { /**
  • Constructor that initialises the cell dimension and the common xhtml attributes
  • @param name The cell name. It'll be used as an ID if you want to use javascript (STRING)
  • @param background_color Background color of this Cell. Use a name or an Hexadecimal value (STRING)
  • @param horizontal_alignment This parameter represents the text horizontal alignement of every cell in the row (INTEGER)
  • and can take these values :
  • <ul>
  • <li> 1 => left (Default Value)</li>
  • <li> 2 => center </li>
  • <li> 3 => right </li>
  • <ul>
  • @param vertical_alignment This parameter represents the text vertical alignement of every cell in the row (INTEGER)
  • and can take these values :
  • <ul>
  • <li> 1 => up </li>
  • <li> 2 => center (Default value)</li>
  • <li> 3 => bottom </li>
  • <li> 4 => baseline (It follows text line) </li>
  • <ul>
  • /
function Row($name = "Row", $background_color = "white", $horizontal_alignement = 1, $vertical_alignement = 2) { //Call the super constructor $this->Element($name, $background_color, $horizontal_alignment); $this->setVerticalAlignment($vertical_alignment); } /**
  • Set the text vertical alignement of every cell in a row.
  • @param vertical_alignment This parameter represents the text alignement (INTEGER)
  • and can take these values :
  • <ul>
  • <li> 1 => up </li>
  • <li> 2 => center (Default value)</li>
  • <li> 3 => bottom </li>
  • <li> 4 => baseline (It follows text line) </li>
  • <ul>
  • /
function setVerticalAlignement($vertical_alignment = 2) { switch($vertical_alignment) { case:1 $this->att["valign"] = "top"; break; case:3 $this->att["valign"] = "bottom"; break; case:4 $this->att["valign"] = "baseline"; break; default $this->att["valign"] = "center"; break; } } /**
  • Add a cell to the row.
  • @param cell A Cell object (CELL CLASS)
  • @return (BOOLEAN) True if the cell has been added - False if an error has occured
  • /
function addCell($cell) { if(isset($cell)) { $this->cells = array_push($this->cells, $cell); return true; } else return false; } /**
  • Add all the cells stored in a array to to the row.
  • @param cells A Cells Array (CELL CLASS INDEXED ARRAY)
  • @return (BOOLEAN) True if the cells have been added - False if an error has occured
  • /
function addCells($cells) { if(isset($cells) && is_array($cells)) { for($i = 0; $i < count($cells); $i++) { $this->cells = array_push($this->cells, $cells[$i]); } return true; } else return false; } /**
  • Add all the value stored in a one dimension array
  • @param cells Cell values Array (STRING INDEXED ARRAY)
  • @return (BOOLEAN) True if the cells have been added - False if an error has occured
  • /
function addCellsCreation($cells) { if(isset($cells) && is_array($cells)) { for($i = 0; $i < count($cells); $i++) { $c = new Cell("Cell ".$i, $cells[$i]); if($this->titles) { $c->setTitle(true); } $this->cells = array_push($this->cells, $c); } return true; } else return false; } /**
  • Set Row as columns titles or not. This is only used when you create a row by
  • passing a string array that contains the values. (addCellsCreation method)
  • @param title True if the row is columns titles - False if not (BOOLEAN)
  • /
function setTitle($title) { $this->titles = $title; } /**
  • Retrieve a cell of the row
  • @param index The index number of the cell you want to retrieve.
  • @return (MIXED) A cell object if the cell exits - False if an error has occured
  • /
function getCell($index) { if(isset($index) && $index >= 1 && $index<=count($this->cells)) { return $this->cells[$index]; } else return false; } /**
  • Remove a cell from the row.
  • @param index The index number of the cell that should be removed. (INTEGER)
  • @return (BOOLEAN) True if the cell has been removed - False if an error has occured
  • /
function removeCell($index) { if(isset($index) && $index >= 1 && $index<=count($this->cells)) { array_splice($this->cells, $index-1, 1); return true; } else return false; } /**
  • Remove all cells from the row.
  • /
function removeCells() { $this->cells = array(); } /**
  • Display the row
  • /
function display() { echo "<tr "; Element::display(); echo ">\n"; for($i = 0; $i < count($this->cells); $i++) { $this->cells[$i]->display(); } echo "</tr>\n"; } /**
  • @var cells Array that will contain all the cells of the row. (INDEXED ARRAY of CELL objects)
  • /
var $cells = array(); /**
  • @var titles True if the row contains columns titles - False if not (BOOLEAN)
  • /
var $titles = false; } /**
  • This class is used to create and handle an xhtml table.
  • /
class Table extends Element { /*
  • Constructor that initialises the table dimension and the common xhtml attributes
  • @param name The table name. It'll be used as an ID if you want to use javascript (STRING)
  • @param background_color Background color of this table. Use a name or an Hexadecimal value (STRING)
  • @param horizontal_alignment This parameter represents the text alignement (INTEGER)
  • and can take these values :
  • <ul>
  • <li> 1 => left (Default Value)</li>
  • <li> 2 => center </li>
  • <li> 3 => right </li>
  • <ul>
  • @param height The table height given in pixel or percent (e.g. "75px" or "20%") (STRING)
  • @param width The table width given in pixel or percent (e.g. "75px" or "20%") (STRING)
  • /
function Table($name = "Cell", $background_color = "white", $horizontal_alignment = 1, $height = "30%", $width = "70%") { //call the super constructor $this->Element($name, $background_color, $horizontal_alignment); $this->att["height"] = $height; $this->att["width"] = $width; } /**
  • Set the background image
  • @param src The absolute filename of the background image (STRING)
  • /
function setBackgroundImage($src) { $this->att["background"] = $src; } /**
  • Set the border width
  • @param border The border width given in pixels (INTEGER)
  • /
function setBorder($border) { $this->att["border"] = $border." px"; } /**
  • Set the table height
  • @param height The table height given in pixel or percent (e.g. "75px" or "20%") (STRING)
  • /
function setHeight($height = "30%") { $this->att["height"] = $height; } /**
  • Set the table width
  • @param width The table width given in pixel or percent (e.g. "75px" or "20%") (STRING)
  • /
function setWidth($width = "70%") { $this->att["width"] = $width; } /**
  • Set the distance between the cell edge and its text
  • @param $padding The distance given in pixels (INTEGER)
  • /
function setPadding($padding = 3) { $this->att["cellpadding"] = $padding." px"; } /**
  • Set the distance between the table edge and its cells
  • @param $space The distance given in pixels (INTEGER)
  • /
function setPadding($space = 3) { $this->att["cellspacing"] = $padding." px"; } /**
  • Set the space left before and after the table
  • @param $horizontal_space The distance given in pixels (INTEGER)
  • /
function setHorizontalSpace($horizontal_space = 15) { $this->att["vspace"] = $horizontal_space." px"; } /**
  • Set the space left at the left and at the right of the table
  • @param $vertical_space The distance given in pixels (INTEGER)
  • /
function setVerticalSpace($vertical_space = 15) { $this->att["hspace"] = $vertical_space." px"; } /**
  • Add a row to the table.
  • @param row A Row object (ROW CLASS)
  • @return (BOOLEAN) True if the row has been added - False if an error has occured
  • /
function addRow($row) { if(isset($row)) { $this->rows = array_push($this->rows, $row); return true; } else return false; } /**
  • Add all the rows stored in a array to the table.
  • @param rows A Rows Array (ROW CLASS INDEXED ARRAY)
  • @return (BOOLEAN) True if the rows have been added - False if an error has occured
  • /
function addRows($rows) { if(isset($rows) && is_array($rows)) { for($i = 0; $i < count($rows); $i++) { $this->rows = array_push($this->rows, $rows[$i]); } return true; } else return false; } /**
  • Add all the values stored in a two dimensions array
  • @param cells Cell values Array (STRING INDEXED ARRAY)
  • @param title_index The index for the row in the array that contains the columns titles. (INTEGER)
  • @return (BOOLEAN) True if the cells have been added - False if an error has occured
  • /
function addCells($cells, $title_index) { if(isset($cells) && is_array($cells)) { //Create the row that contains the columns titles $r = new Row("Row 1"); $r->addCellsCreation($cells[$title_index]); $r->setTitle(true); //Add it to the table $this->rows = array_push($this->rows, $r); //Loop through the rows of the array for($i = 0; $i < count($cells); $i++) { //Check it isn't the row that contains the columns titles if($i != $title_index) { //Create a row $r = new Row("Row ".$i); //Create the cells of this rows $r->addCellsCreation($cells[$i]); $r->setTitle(slase); //Add the row to the table $this->rows = array_push($this->rows, $r); } } return true; } else return false; } /**
  • Retrieve a row of the table
  • @param index The index number of the row you want to retrieve.
  • @return (MIXED) A row object if the row exits - False if an error has occured
  • /
function getRow($index) { if(isset($index) && $index >= 1 && $index<=count($this->rows)) { return $this->rows[$index]; } else return false; } /**
  • Retrieve a cell of the table
  • @param row_index The index number of the row that contain the cell you want to retrieve.
  • @param col_index The index number of the col that contain the cell you want to retrieve.
  • @return (MIXED) A cell object if the cell exits - False if an error has occured
  • /
function getCell($row_index, $col_index) { if(isset($row_index) && $row_index >= 1 && $row_index<=count($this->rows)) { //Get the row $r = $this->getRow($row_index); //Return the cell or false if an error occured return $r->getCell($col_index); } else return false; } /**
  • Display the table
  • /
function display() { //Display the open tag echo "<table "; Element::display(); echo ">\n"; //Loop throught the rows of this table for($i = 0; $i < count($this->rows); $i++) { //Get the row $r = $this->getRow[$i]; //Display the row $r->display(); } //Close the table tag echo "</table>\n"; } /**
  • @var rows Array that will contain all the rows of the table. (INDEXED ARRAY of ROW objects)
  • /
var $rows = array(); } ?>

Codes Sources

A voir également

Ajouter un commentaire

Commentaire

cs_callman
Messages postés
12
Date d'inscription
jeudi 27 mars 2003
Statut
Membre
Dernière intervention
18 décembre 2006
-
J'aime bien ta source .. sympa, je vais peut etre la modifier un peu pour la mettre sur mon site !

Merci en tout cas, ça simplifie bien la vie ;)
Bonne continuation.

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.