Génération du code html pour un tableau (hérite de la classe nd_template)

Description

Extension du moteur de template (ND_Template) afin de générer le code HTML d'un tableau bi-dimensionnel.
Cela génère le header du tableau avec la valeurs des clés.

Nécessite forcement la classe ND_Template.

Cette classe peut etre pratique pour afficher un retour de BDD sous forme de tableau ou tout autre tableau associatif

exemple :

Source / Exemple :


<?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 ;

Codes Sources

A voir également

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.