Simple table function

Contenu du snippet

Petite fonction simple sans pretention.... On lui passe 2 arrays (1 pour les entetes et 1 autre pour les data) avec en option le nom d'un style pour l'entete et pour la table elle-meme...
la fonction n'affice rien mais renvoi un tableau qu'il suffit ensuite d'imprimer.

Source / Exemple :


<style>
TABLE.smtTable{
	border-left:1px black solid;
	border-right:1px solid black;
	border-bottom:1px solid black;
	border-top:1px solid black;
	margin-right:15px;
	width:800px;
	font-size:11pt;
}

TR.smtRowOdd{
	
	background-color:lightgrey;
}
TR.smtRowEven{
	background-color:white;
}
TR.smtHeader{
	background-color:#2d1663;
	background-color:#F0F0F0;
	color:#2d1663;
	font-weight:bold;
	font-size:13pt;
}
</style>

<?php

function DisplayTable($tablehead,$tabledata,$styletable='smtTable',$stylehead='smtHeader')
{
	
	//Creating HTML table 
	$table='<table class=' . $styletable .'>'."\n";
	$table.='<tr class=' . $stylehead . '>' . "\n";
	
	//now displaying table head
	$col=count($tablehead);
	
	//Looping inside tableheaders and displaying them
	foreach ($tablehead as $t)
	{
		if (empty($t)) {$t='_';}$table.='<td>' .$t.'</td>'."\n";
	}
	$table.='</tr>'."\n";
	
	
	//now displaying data with row colors
	$numel=count($tabledata); //amount of cells
	$rows=intval($numel/$col);  // line count is cells/col
	$pointeur=0;   //pointeur for data
	//first loop, we are going through each row, row length is based on number of Header element
	for ($i=0;$i<$rows;$i++)
	{
		//changing row color.  Using modulo function (always returns 1 or 0)
		if ($i%2 == 0) $s="smtRowEven"; else $s="smtRowOdd";
		$table.='<tr class=' . $s . '>' . "\n";
		//We display each cells in the row.  Number of Colums is based on number of header as set above
		for ($j=0;$j<$col;$j++)
		{
			$d=$tabledata[$pointeur];
			
			if (empty($d)) {$d='_';}$table.='<td >' . $d . '</td>' . "\n";
			$pointeur++; // moving to next element on tableset
		}
		$table.='</tr>'."\n";
	}
	$table.='</table>'."\n";
	return $table;
}

$head=array("head1","head2","head3","test1");
$data=array("r1d1","r1d2","r2d1","r2d2","r3d1","r3d2","r2d2","r3d1","r3d2");
$d=DisplayTable($head,$data);
print $d;
print "done";

?>

Conclusion :


J'ais cree ce code car j'avais besoin d'une petite fonction me permettant d'afficher simplement un tableau sans fioriture (essentiellement 1 ou 2 lignes)... Suggestion/amelioration sont bien entendu les bienvenues ...

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.