Scripts

HTML table Class

Met de class kun je heel makkelijk en snel simpele HTML tabellen maken. Ikzelf had dit nodig voor een eigen website, omdat ik veel met informatietabelletjes werk en ik geen zin heb om 100 regels aan in mijn code te hebben. Het script is niet groot en dus nog makkelijk zelf aan te passen. Op dit moment is het toegespitst op wat ik nodig had, dus wellicht wil je in de constructor wat "instellingen" veranderen. Volgens mij spreekt het script zo voor zich dat commentaar in de code niet nodig is. Het script maakt gebruik van 3 CSS-classes (cl_table, cl_th en cl_td). Ik gebruik: Voor PHP 5 pak je de bovenste class, voor PHP 4 de onderste.

html-table-class
[b]De class (PHP 5)[/b]
<?php

class Table 
{
	const NL = "\r\n"; # newline om de HTML-code overzichtelijk te houden
	
	public function __construct($width = 600, $extra = '') {
		echo self::NL . '<table class="cl_table" cellspacing="0" cellpadding="5" width="' .$width. '" ' .$extra. '>' . self::NL;
	}
	
	public function th() {
		echo '<tr>' . self::NL;
		
		foreach (func_get_args() as $value ) {
			echo '<th class="cl_th">' .$value. '</th>' . self::NL;
		}
		
		echo '</tr>' . self::NL;
	}
	
	public function td() {
		echo '<tr>' . self::NL;
		
		foreach (func_get_args() as $value ) {
			if (!is_array($value) ) {
				echo '<td class="cl_td">' .$value. '</td>' . self::NL;
			}
			else {
				echo '<td class="cl_td" ' .$value[1]. '>' .$value[0]. '</td>' . self::NL;
			}			
		}
		
		echo '</tr>' . self::NL;
	}
	
	public function close() {
		echo '</table>' . self::NL . self::NL;
	}
}

?>

[b]Gebruik van de class[/b]
<?php

$table = new Table; # maak tabel aan 
$table->th('Naam', 'Adres', 'Woonplaats'); # maak kopjes
$table->td('Jan Janssen', 'Kalverstraat 1', 'Amsterdam'); # voeg rij toe
$table->td('Klaas Klaassen', 'Binnenhof 1', 'Den Haag'); # voeg rij toe
$table->td('Piet Pietersen', 'Euromast 1', 'Rotterdam'); # voeg rij toe
$table->close(); # sluit tabel

?>

Of bijvoorbeeld:

<?php

$table = new Table(200); # maak tabel aan van 200px breed
$table->th('Naam'); # maak kopjes
$table->td('Jan Janssen'); # voeg rij toe
$table->td('Klaas Klaassen'); # voeg rij toe
$table->td('Piet Pietersen'); # voeg rij toe
$table->close(); # sluit tabel

?>


[b]Extra attributen[/b]
Als je bij slechts één <td> een toevoeging wilt, dan kan dat door een array te gebruiken, dus bijvoorbeeld:

<?php
$table->td( array('Piet Pietersen', 'bgcolor="#EEEEEE"'), 'Euromast 1', 'Rotterdam'); # voeg rij toe
?>

Een toevoeging aan de <table> kan door een tweede argument mee te geven aan de class zelf.

[b]Usage[/b]
mixed [b]Table[/b] ( [int width] [, string html_attribute] )
mixed [b]th[/b] ( mixed column_name [, mixed ...] ] )
mixed [b]td[/b] ( mixed column_value [, mixed ...]  )
mixed [b]close[/b] ( void )




[b]Class in PHP 4 (werking hetzelfde)[/b]
[i]Thank you Eris[/i]
<?php
define('NL',"\r\n"); # newline om de HTML-code overzichtelijk te houden
class Table 
{
    function Table($width = 600, $extra = ''){
        echo NL . '<table class="cl_table" cellspacing="0" cellpadding="5" width="' .$width. '" ' .$extra. '>' . NL;
    }
     function th() {
        echo '<tr>' . NL;
        
        foreach (func_get_args() as $value ) {
            echo '<th class="cl_th">' .$value. '</th>' . NL;
        }
        
        echo '</tr>' . NL;
    }
    
    function td() {
        echo '<tr>' . NL;
        
        foreach (func_get_args() as $value ) {
            if (!is_array($value) ) {
                echo '<td class="cl_td">' .$value. '</td>' . NL;
            }
            else {
                echo '<td class="cl_td" ' .$value[1]. '>' .$value[0]. '</td>' . NL;
            }            
        }
        
        echo '</tr>' . NL;
    }
    
    function close() {
        echo '</table>' . NL . NL;
    }
}


$table = new Table; # maak tabel aan 
$table->th('Naam', 'Adres', 'Woonplaats'); # maak kopjes
$table->td('Jan Janssen', 'Kalverstraat 1', 'Amsterdam'); # voeg rij toe
$table->td('Klaas Klaassen', 'Binnenhof 1', 'Den Haag'); # voeg rij toe
$table->td('Piet Pietersen', 'Euromast 1', 'Rotterdam'); # voeg rij toe
$table->close(); # sluit tabel

?> 

Reacties

0
Nog geen reacties.