- <?php
-
- require_once 'ND/Template.php';
-
- class ND_Template_HTML_Table extends ND_Template{
- private $array ;
- private $header ;
- private $caption ;
- private $attributes ;
- private $footer ;
- private $tbody ;
- private $nbCol = 0 ;
- const TABLE_PATTERN = '
- <table #attributs#>
- #caption#
- #thead#
- #tbody#
- #tfoot#
- </table>';
-
- public function __construct($array){
- $this->setArray($array);
- $this->_content = self::TABLE_PATTERN;
- }
-
- public function setArray($array){
- if ( ! is_array($array) ){
- throw new Exception('$array must be an array');
- }
- $this->array = $array ;
- return $this;
- }
-
- private function prepare(){
-
- if ( count($this->array) > 0 ){
- $keys = array_keys($this->array[0]);
- $sKey = '';
- foreach($keys as $key){
- $sKey .= '<th>'.$key.'</th>';
- $this->nbCol++;
- }
- $this->header = '<thead><tr>'.$sKey.'</tr></thead>';
- $sTbody = '';
- foreach( $this->array as $key=>$elt){
- $sTr = '';
- foreach($elt as $item){
- $sTr .= '<td>'.$item.'</td>';
- }
- $sTbody .= '<tr>'.$sTr.'</tr>';
- }
- $this->tbody = '<tbody>'.$sTbody.'</tbody>';
- }
- }
-
- public function setFooter($value){
- if ( ! empty($value) ){
- $this->footer = '<tfoot><tr><td colspan="'.$this->nbCol.'">'.$value.'</td></tr></tfoot>';
- }
- return $this;
- }
-
- public function setAttributs($array){
- if ( ! is_array($array) ){
- throw new Exception('$item must be an array');
- }else{
- foreach( $array as $key=>$value){
- $this->attributes = ' '.$key.'="'.$value.'" ';
- }
- }
- return $this;
- }
-
- public function __toString(){
- $this->prepare();
- $this->addValue('attributs', $this->attributes)
- ->addValue('caption', $this->caption)
- ->addValue('thead', $this->header)
- ->addValue('tbody', $this->tbody)
- ->addValue('tfoot', $this->footer);
- return parent::__toString();
- }
-
- public function setCaption($value){
- if ( false === empty($value) ){
- $this->caption = '<caption>'.$value.'</caption>';
- }
- return $this;
- }
-
- }
-
- // Exemple d'utilisation
- // on considère qu'une connexion a été établie et que
- $oDB->execute('Select id, login, mail, pass From User'); // on execute une requete
- $array = $oDB->result->toArray(); // renvoi le jeu de resultat sous forme de tableau associatif (cf Zend_Table du Zend Framework)
- $tableauHTML = new ND_Template_HTML_Table($array);
- echo $tableauHTML ;
<?php
require_once 'ND/Template.php';
class ND_Template_HTML_Table extends ND_Template{
private $array ;
private $header ;
private $caption ;
private $attributes ;
private $footer ;
private $tbody ;
private $nbCol = 0 ;
const TABLE_PATTERN = '
<table #attributs#>
#caption#
#thead#
#tbody#
#tfoot#
</table>';
public function __construct($array){
$this->setArray($array);
$this->_content = self::TABLE_PATTERN;
}
public function setArray($array){
if ( ! is_array($array) ){
throw new Exception('$array must be an array');
}
$this->array = $array ;
return $this;
}
private function prepare(){
if ( count($this->array) > 0 ){
$keys = array_keys($this->array[0]);
$sKey = '';
foreach($keys as $key){
$sKey .= '<th>'.$key.'</th>';
$this->nbCol++;
}
$this->header = '<thead><tr>'.$sKey.'</tr></thead>';
$sTbody = '';
foreach( $this->array as $key=>$elt){
$sTr = '';
foreach($elt as $item){
$sTr .= '<td>'.$item.'</td>';
}
$sTbody .= '<tr>'.$sTr.'</tr>';
}
$this->tbody = '<tbody>'.$sTbody.'</tbody>';
}
}
public function setFooter($value){
if ( ! empty($value) ){
$this->footer = '<tfoot><tr><td colspan="'.$this->nbCol.'">'.$value.'</td></tr></tfoot>';
}
return $this;
}
public function setAttributs($array){
if ( ! is_array($array) ){
throw new Exception('$item must be an array');
}else{
foreach( $array as $key=>$value){
$this->attributes = ' '.$key.'="'.$value.'" ';
}
}
return $this;
}
public function __toString(){
$this->prepare();
$this->addValue('attributs', $this->attributes)
->addValue('caption', $this->caption)
->addValue('thead', $this->header)
->addValue('tbody', $this->tbody)
->addValue('tfoot', $this->footer);
return parent::__toString();
}
public function setCaption($value){
if ( false === empty($value) ){
$this->caption = '<caption>'.$value.'</caption>';
}
return $this;
}
}
// Exemple d'utilisation
// on considère qu'une connexion a été établie et que
$oDB->execute('Select id, login, mail, pass From User'); // on execute une requete
$array = $oDB->result->toArray(); // renvoi le jeu de resultat sous forme de tableau associatif (cf Zend_Table du Zend Framework)
$tableauHTML = new ND_Template_HTML_Table($array);
echo $tableauHTML ;