begin process at 2010 03 22 16:34:08
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Divers

 > SIMPLE TABLE FUNCTION

SIMPLE TABLE FUNCTION


 Information sur la source

Note :
7,5 / 10 - par 2 personnes
7,50 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Divers Niveau :Débutant Date de création :07/04/2005 Date de mise à jour :12/04/2005 14:38:24 Vu :2 608

Auteur : galadriann

Ecrire un message privé
Site perso
Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

 Description

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

  • <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";
  • ?>
<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 ...


 Historique

12 avril 2005 14:29:50 :
Mise a jour du code avec les suggestions ... merci pour les commentaires...
12 avril 2005 14:38:24 :
Mise a jour du code avec les suggestions ... merci pour les commentaires...

 Sources du même auteur

Source avec Zip CLASS HELPER
COMMENT SEPARER DES VALEURS INDEX DANS UNE VARIABLE CSV

 Sources de la même categorie

CALCUL D'UNE DISTANCE ORTHONORMIQUE par bossfoot
Source avec Zip ESPACE ADMIN SIMPLE par mousaid_88
Source avec Zip IMAGINE-CMS V2.20 par djack69
Source avec Zip AFFICHAGE ET GESTION DE DIAPORAMA EN PHP SANS BASE DE DONNÉE... par mldvb
Source avec Zip Source avec une capture PARSER ALLOCINE par cyrhades

Commentaires et avis

Commentaire de stepibou le 07/04/2005 10:55:52

sympatique!
enfin une source, qui peux servir,
apprendre des trucs aux autres,
et qui m'as l'air plutot bien codé.

voila , je montre aussi que je suis pas tj là
pour casséeeer / les autres.

sinon, un poil plus d'explication ne ferait peut
etre pas de mal.

voila , tchuss.
je te noterai correctement, si personne le fait.

Commentaire de JoJo738 le 07/04/2005 16:23:39

Lu, bonne idée.
Mais il y a possibilité de fortement l'amelioré :
  -  Argument html entre " "
  -  priviligié les ' aux " ( array('head1', 'head2', 'head3 ', 'test1'); -  $table.= '<tr class="' . $s . '">' . "\n"; ou "tr class=\"' . $s . '\">\n" ...)
Sinon bien dans l'esemble :p

Commentaire de SebLep le 08/04/2005 17:20:16

Salut, une petite erreur : la division "/" en php n'est pas
une division entière...
remplace $rows=$numel/$col; par $rows=intval($numel/$col);
cela evite d'avoir l'erreur :
Notice: Undefined offset: 9

Sinon bien

Commentaire de galadriann le 12/04/2005 14:44:27

Merci pour les commentaires ...
Je viens de remettre le code a jour ...
Les ' au lieu des " c'est vrai que c'est mieux mais je trouvais un peu lourd comme j'aime bien mettre des "\n" a la fin des lignes, histoire d'avoir un code html lisible ...

J'ai rajoute quelques commentaires ... (desole mais je code toujours en anglais ... mais je veux bien traduire si cela pose probleme ..)

Merci

 Ajouter un commentaire




Nos sponsors


Appels d'offres

Sondage...

CalendriCode

Mars 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
293031    

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,593 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales