par
Cyrano » 22 avr. 2006, 08:40
Salut : petits conseils : sépare donc le PHP du html et aligne proprement tout ça, ça fera moins "torchon" et tu auras beaucoup moins de mal à débugguer si nécessaire : Pour ton texte alternatif, ce n'est pas sorcier : d'abord déplacer la vérification de l'existence d'un fichier image et ensuite on fait la même chose pour déterminer si on met un texte alternatif : j'ai modifié et tajouté une ligne ou deux avec commentaires au tout début de la boucle while :
<?php
// on se connecte à MySQL
$db = mysql_connect('localhost', 'ecole', 'xxxx');
// on sélectionne la base
mysql_select_db('ecole',$db);
$numeroPage = isset($_GET['numeroPage']) ? $_GET['numeroPage'] : 0;
$numeroPagePrecedent = $numeroPage - 1;
$numeroPageSuivant = $numeroPage + 1;
// numero de la page à afficher, commencer à 0
$nbElementParPage = 5; // nombre d'éléments par page
$indexAffiche = $numeroPage * $nbElementParPage; // index à partir du quel on va afficher les éléments.
//Donc en fonction du numero de la page, tu obtiendras 0*10 = 0; 1*10=10; 2*10=20 etc.
// on crée la requête SQL
$sql = "SELECT ID,nom,lieu,adresse,metro,tel,mail,site,info FROM ecoles_tbl ORDER BY gpe ASC LIMIT $indexAffiche , $nbElementParPage";
// dossier image
$dossier_img = './upload/';
$chemin_image = $dossier_img.'logo_cours_'.$data['ID'].gif;
$extension = '.gif';
// on envoie la requête
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
// on fait une boucle qui va faire un tour pour chaque enregistrement
while($data = mysql_fetch_assoc($req))
{
/* Ici on détermine s'il y a une image ou si on affiche l'image par défaut */
$image = (file_exists($dossier_img.'logo_cours_'.$data['ID'].$extension) ) ? 'logo_cours_'.$data['ID'].$extension : 'no_logo.gif' ;
/* Si on a une image, récupération du texte alternatif */
$alt = (file_exists($dossier_img.'logo_cours_'.$data['ID'].$extension) ) ? "" : null;
// on affiche les informations de l'enregistrement en cours
?>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<TD width="21"><IMG height=50 alt="" src="../logos/tlc.gif" width=20 border=0 name=tlc></TD>
<TD width="678" background="../logos/tm.gif" colspan="2"><IMG height=1 alt="" src="../logos/spacer.gif" width=1 border=0 name=tm></TD>
<TD width="21"><IMG height=50 alt="" src="../logos/trc.gif" width=20 border=0 name=trc></TD>
</tr>
<tr>
<TD background="../logos/left.gif" rowspan="3" width="21"><IMG height=1 alt="" src="../logos/spacer.gif" width=1 border=0 name=left></TD>
<TD vAlign=middle bgColor=#000000 align="center" width="100%">
<font color="#FF7E00"><b><?php echo($data['nom']); ?></b></font>
<hr>
</TD>
<TD vAlign=middle bgColor=#000000 align="center">
<img src="<?php echo($dossier_img . $image); ?>" width="111" height="46" border="0" alt="<?php echo($alt); ?>" />
</TD>
<TD background="../logos/right.gif" rowspan="3" width="21"><IMG height=1 alt="" src="../logos/spacer.gif" width=1 border=0 name=right></TD>
</tr>
<tr>
<TD vAlign=middle bgColor=#000000 align="center" width="678" colspan="2">
<b><?php echo($data['lieu']); ?></b><br>
Contact <?php echo($data['tel']); ?><br>
Adresse <?php echo($data['adresse']); ?><br>
Métro <?php echo($data['metro']); ?><br>
Mail : <a href="mailto:<?php echo($data['mail']); ?>"><?php echo($data['mail']); ?></a><br>
Site internet : <a href="http://<?php echo($data['site']); ?>" target="_blank"><?php echo($data['site']); ?></a>
<hr>
</TD>
</tr>
<tr>
<TD vAlign=middle bgColor=#000000 width="100%" colspan="2">
<u>Infos supplémentaires</u> : <h5><?php echo($data['info']); ?></h5>
</TD>
</tr>
<tr>
<TD width="21"><IMG height=30 alt="" src="../logos/blc.gif" width=20 border=0 name=blc></TD>
<TD background="../logos/btm.gif" width="678" colspan="2"><IMG height=1 alt="" src="../logos/spacer.gif" width=1 border=0 name=btm></TD>
<TD width="21"><IMG height=30 alt="" src="../logos/brc.gif" width=20 border=0 name=brc></TD>
</tr>
</table>
</DIV>
<?php
}
/* !ATTENTION : il manque un if() ou quelque chose du genre ICI */
{
// on affiche les informations de l'enregistrement en cours
echo '<a href="?numeroPage='.$numeroPagePrecedent.'"><p align="center">Précédent</a>
<a href="?numeroPage='.$numeroPageSuivant.'">Suivant</a></p>';
}
// on ferme la connexion à mysql
mysql_close();
?>
Salut : petits conseils : sépare donc le PHP du html et aligne proprement tout ça, ça fera moins "torchon" et tu auras beaucoup moins de mal à débugguer si nécessaire : Pour ton texte alternatif, ce n'est pas sorcier : d'abord déplacer la vérification de l'existence d'un fichier image et ensuite on fait la même chose pour déterminer si on met un texte alternatif : j'ai modifié et tajouté une ligne ou deux avec commentaires au tout début de la boucle while :
[php]<?php
// on se connecte à MySQL
$db = mysql_connect('localhost', 'ecole', 'xxxx');
// on sélectionne la base
mysql_select_db('ecole',$db);
$numeroPage = isset($_GET['numeroPage']) ? $_GET['numeroPage'] : 0;
$numeroPagePrecedent = $numeroPage - 1;
$numeroPageSuivant = $numeroPage + 1;
// numero de la page à afficher, commencer à 0
$nbElementParPage = 5; // nombre d'éléments par page
$indexAffiche = $numeroPage * $nbElementParPage; // index à partir du quel on va afficher les éléments.
//Donc en fonction du numero de la page, tu obtiendras 0*10 = 0; 1*10=10; 2*10=20 etc.
// on crée la requête SQL
$sql = "SELECT ID,nom,lieu,adresse,metro,tel,mail,site,info FROM ecoles_tbl ORDER BY gpe ASC LIMIT $indexAffiche , $nbElementParPage";
// dossier image
$dossier_img = './upload/';
$chemin_image = $dossier_img.'logo_cours_'.$data['ID'].gif;
$extension = '.gif';
// on envoie la requête
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
// on fait une boucle qui va faire un tour pour chaque enregistrement
while($data = mysql_fetch_assoc($req))
{
/* Ici on détermine s'il y a une image ou si on affiche l'image par défaut */
$image = (file_exists($dossier_img.'logo_cours_'.$data['ID'].$extension) ) ? 'logo_cours_'.$data['ID'].$extension : 'no_logo.gif' ;
/* Si on a une image, récupération du texte alternatif */
$alt = (file_exists($dossier_img.'logo_cours_'.$data['ID'].$extension) ) ? "" : null;
// on affiche les informations de l'enregistrement en cours
?>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<TD width="21"><IMG height=50 alt="" src="../logos/tlc.gif" width=20 border=0 name=tlc></TD>
<TD width="678" background="../logos/tm.gif" colspan="2"><IMG height=1 alt="" src="../logos/spacer.gif" width=1 border=0 name=tm></TD>
<TD width="21"><IMG height=50 alt="" src="../logos/trc.gif" width=20 border=0 name=trc></TD>
</tr>
<tr>
<TD background="../logos/left.gif" rowspan="3" width="21"><IMG height=1 alt="" src="../logos/spacer.gif" width=1 border=0 name=left></TD>
<TD vAlign=middle bgColor=#000000 align="center" width="100%">
<font color="#FF7E00"><b><?php echo($data['nom']); ?></b></font>
<hr>
</TD>
<TD vAlign=middle bgColor=#000000 align="center">
<img src="<?php echo($dossier_img . $image); ?>" width="111" height="46" border="0" alt="<?php echo($alt); ?>" />
</TD>
<TD background="../logos/right.gif" rowspan="3" width="21"><IMG height=1 alt="" src="../logos/spacer.gif" width=1 border=0 name=right></TD>
</tr>
<tr>
<TD vAlign=middle bgColor=#000000 align="center" width="678" colspan="2">
<b><?php echo($data['lieu']); ?></b><br>
Contact <?php echo($data['tel']); ?><br>
Adresse <?php echo($data['adresse']); ?><br>
Métro <?php echo($data['metro']); ?><br>
Mail : <a href="mailto:<?php echo($data['mail']); ?>"><?php echo($data['mail']); ?></a><br>
Site internet : <a href="http://<?php echo($data['site']); ?>" target="_blank"><?php echo($data['site']); ?></a>
<hr>
</TD>
</tr>
<tr>
<TD vAlign=middle bgColor=#000000 width="100%" colspan="2">
<u>Infos supplémentaires</u> : <h5><?php echo($data['info']); ?></h5>
</TD>
</tr>
<tr>
<TD width="21"><IMG height=30 alt="" src="../logos/blc.gif" width=20 border=0 name=blc></TD>
<TD background="../logos/btm.gif" width="678" colspan="2"><IMG height=1 alt="" src="../logos/spacer.gif" width=1 border=0 name=btm></TD>
<TD width="21"><IMG height=30 alt="" src="../logos/brc.gif" width=20 border=0 name=brc></TD>
</tr>
</table>
</DIV>
<?php
}
/* !ATTENTION : il manque un if() ou quelque chose du genre ICI */
{
// on affiche les informations de l'enregistrement en cours
echo '<a href="?numeroPage='.$numeroPagePrecedent.'"><p align="center">Précédent</a>
<a href="?numeroPage='.$numeroPageSuivant.'">Suivant</a></p>';
}
// on ferme la connexion à mysql
mysql_close();
?>[/php]