Page 1 sur 1

Aide optimisation script php

Posté : 17 nov. 2008, 12:05
par Invité
Bonjour à tous,

Je cherche une façon d'optimiser un script php.

Je débute pour le coup et je n'ai pas vraiment d'idée sur la façon de faire.

Voici mon bout de code tableau.php :

Code : Tout sélectionner

<HTML> <head> <style> BODY { font-family : Verdana,Arial; } TD { font-family : Verdana,Arial; font-size : 8pt; } A { text-decoration : none; background-color : #A8D3ED; } </style> </head> <body bgcolor=#a8d3ed link=#339933> <br> <?php if(empty($type)) { $type = "$nom"; } echo " <table border=\"1\"> <CAPTION>Titre de mon tableau</CAPTION> <tr> <th colspan=\"1\" rowspan=\"3\">date</th> <th colspan=\"1\" rowspan=\"3\"><a href=\"http://monsite/cgi-bin/monurl&type=$type&active=\">total</a></th> <th colspan=\"7\" rowspan=\"1\">Type : $type</td></tr> <tr><th colspan=\"3\" rowspan=\"1\">Active</td> <th colspan=\"4\" rowspan=\"1\">Inactive</td></tr> <tr><th><a href=\"http://monsite/cgi-bin/monurl&type=$type&active=^active\">total active</a></th> <th><a href=\"http://monsite/cgi-bin/monurl&type=$type&active=^active&etat=production\">en production</a></th> <th><a href=\"http://monsite/cgi-bin/monurl&type=$type&active=^active&etat=test\">en test</a></th> <th><a href=\"http://monsite/cgi-bin/monurl&type=$type&active=inactive\">total inactive</a></th> <th><a href=\"http://monsite/cgi-bin/monurl&type=$type&active=inactive&etat=param\">en parametrage</a></th> <th><a href=\"http://monsite/cgi-bin/monurl&type=$type&active=inactive&etat=production\">en production</a></th> <th><a href=\"http://monsite/cgi-bin/monurl&type=$type&active=inactive&etat=etat2\">en demande de valid</th></tr> </tr>"; // on recupere la variable $lignes = file("$nom"); // traitement de chaque ligne for ($i=0; $i<count($lignes); $i++) { $couleur_ligne = ($i % 2) ? '#B8C8FE' : '#EEEEEE'; echo "<tr>"; $statistiques=explode(" ",$lignes[$i]); for ($a=0; $a<count($statistiques); $a++){ echo "<td bgcolor=\"".$couleur_ligne."\">".$statistiques[$a]; } echo "</tr>"; } echo "</table> <br> <A Href=\"export.php\"><u>Exporter le tableau</u></A>"; ?> </BODY></HTML>
Il doit y avoir quelque chose à faire au niveau de l'entête de mon tableau, là où il y a une succession de lien html.

Merci !!

Posté : 17 nov. 2008, 13:44
par AB
Peut-être tu pourrais utiliser les balises <ul> et <li> si tes liens peuvent s'apparenter à une liste mais ça c'est plus du domaine de html. C'est vrai qu'au niveau html ton code est loin d'être parfait : pas de déclaration de doctype ni autre, c'est vraiment le minimum. Pour avoir des documents valides il faudrait que tu ailles faire un tour sur http://www.alsacreations.com/ qui parle bien du sujet et qui te montrera comment utiliser plus souvent les feuilles de style de préférence à des styles incorporés.

Concernant l'organisation de ton code on préconise de séparer autant que possible le code PHP du code HTML... Et dans ton cas est-il utile (ou indispensable) de faire un echo sur tout ton bloc ?

Par exemple ton même code mieux structuré
<?php
if(empty($type)) 
	{
		$type = "$nom";
	}
	
$lignes = file("$nom"); 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document sans titre</title>
<style>
	body { 
	font-family : Verdana,Arial; 
	}
	td { 
	font-family : Verdana,Arial; 
	font-size : 8pt; 
	}
	a { 
	text-decoration : none; 
	background-color : #A8D3ED; 
	}
</style>

</head>

<body>
<table border="1">
<caption>Titre de mon tableau</caption>

	<tr>
		<th colspan="1" rowspan="3">date</th>
		<th colspan="1" rowspan="3"><a href="http://monsite/cgi-bin/monurl&type=<?php echo $type?>&active=">total</a></th>
		<th colspan="7" rowspan="1">Type : <?php echo $type ?></th>
	</tr>
	
	<tr>
		<th colspan="3" rowspan="1">Active</th>
		<th colspan="4" rowspan="1">Inactive</th>
	</tr>
	
	<tr>
		<th><a href="http://monsite/cgi-bin/monurl&type=<?php echo $type?>&active=^active">total active</a></th>
		<th><a href="http://monsite/cgi-bin/monurl&type=<?php echo $type?>&active=^active&etat=production">en production</a></th>
		<th><a href="http://monsite/cgi-bin/monurl&type=<?php echo $type?>&active=^active&etat=test">en test</a></th>
		<th><a href="http://monsite/cgi-bin/monurl&type=<?php echo $type?>&active=inactive">total inactive</a></th>
		<th><a href="http://monsite/cgi-bin/monurl&type=<?php echo $type?>&active=inactive&etat=param">en parametrage</a></th>
		<th><a href="http://monsite/cgi-bin/monurl&type=<?php echo $type?>&active=inactive&etat=production">en production</a></th>
		<th><a href="http://monsite/cgi-bin/monurl&type=<?php echo $type?>&active=inactive&etat=etat2">en demande de valid</a></th>
	</tr>

<?php for ($i=0; $i<count($lignes); $i++) 
		{
        	$couleur_ligne = ($i % 2)? '#B8C8FE' : '#EEEEEE';
			$statistiques = explode(" ",$lignes[$i]);
		
        	echo '<tr>';
        
        	for ($a=0; $a<count($statistiques); $a++)
				{
         			echo '<td bgcolor="'.$couleur_ligne.'">'.$statistiques[$a].'</td>';
        		}
			echo '</tr>';
		}?>
</table>
<br />
<a href="export.php"><u>Exporter le tableau</u></a>
</body>
</html>
Maintenant il se pose un problème au niveau de ta variable "$nom". Ton code laisse à penser que tu travailles avec register_global activé or c'est déconseillé. Il faut définir les variables venant de l'extérieur par exemple si $nom est une variable GET
$nom = isset($_GET['nom'])? $_GET['nom'] : '';
Idem pour $type