Page 1 sur 1

Afficher une image

Posté : 11 déc. 2008, 11:15
par Imer2008
Bonjour,

J'aurais besoin de votre aide sur un petit problème.

J'ai un dossier "personnel" qui contient des images de personnel de notre entreprise.
J'ai un fichier annuaire.php dans lequel je fais afficher l'image en fonction du champ "id" de l'agent.
En gros, si dans mon dossier "personnel" j'ai une image 5.jpg et dans ma base de données j'ai un agent dont l'id est 5, ca m'affiche l'image. Voci le code :

Code : Tout sélectionner

$sql = "SELECT idagent, nomagent,prenomagent,service,fonction,portefeuille,telephone,email FROM agent "; if (!empty($caisse_origine)) {$sql .= "WHERE caisse_origine = '$caisse_origine' ";} $sql .= "ORDER BY $ref"; $resultat = mysql_query ($sql, $connexion); while ($tabagent = mysql_fetch_array($resultat, MYSQL_ASSOC)) { ?> <tr bordercolor="#000000" onMouseOver="this.style.backgroundColor = '#FFCCCC'" onMouseOut="this.style.backgroundColor = '#FFFFFF'"> <td><span class="Style95 Style97"><img src="../personnel/<?php echo $tabagent[idagent]; ?>.jpg" border="1" hspace="10" width="50" align="absmiddle" />&nbsp;<?php echo $tabagent[nomagent]; ?>&nbsp;<?php echo $tabagent[prenomagent]; ?></span></td>
Le hic, c'est que si je n'ai pas d'image correspondant à l'id, ben ca m'affiche une belle croix rouge.
J'ai téléchargé une image "no picture" nommée 0.jpg et donc ce que je souhaiterais, c'est que si il n'y a pas d'image correspondant à l'id que ca m'affiche l"image "no picture".

Pourriez-vous m'aider ?

D'avance merci.

Posté : 11 déc. 2008, 11:33
par yaug
Bonjour.

La fonction is_file() est ton amie.

Voici comment t'en servir dans ton cas :
<?php
$sql = "SELECT idagent, nomagent,prenomagent,service,fonction,portefeuille,telephone,email FROM agent ";
if (!empty($caisse_origine)) {$sql .= "WHERE caisse_origine = '$caisse_origine' ";}
$sql .= "ORDER BY $ref";
$resultat = mysql_query ($sql, $connexion);
$string = "";
while ($tabagent = mysql_fetch_array($resultat, MYSQL_ASSOC)) { 
	$string .= "<tr bordercolor=\"#000000\" onMouseOver=\"this.style.backgroundColor = '#FFCCCC'\" onMouseOut=\"this.style.backgroundColor = '#FFFFFF'\">
					<td><span class=\"Style95 Style97\">";
	$image = "../personnel/".$tabagent['idagent'].".jpg";
	if(is_file($image)) $string.= "<img src=\"$image\" border=\"1\" hspace=\"10\" width=\"50\" align=\"absmiddle\" />";
	$string .= " ".$tabagent['nomagent']." ".$tabagent['prenomagent']."</span></td>";
}
echo $string;
							
								
?>

Posté : 11 déc. 2008, 12:06
par Imer2008
Bonjour yaug,

Merci de ton aide. J'ai testé ton code et donc j'obtiens que si l'image existe alors elle s'affiche sinon je n'ai rien.

Or je souhaiterais avoir l'image 0.jpg à la place.
Est-ce possible ?

D'avance merci.

Posté : 11 déc. 2008, 12:10
par yaug
Bien sur :wink:

Fait ceci :
$image = "../personnel/".$tabagent['idagent'].".jpg"; 
$image0 = "../personnel/image0.jpg"; 
 if(is_file($image)) $string.= "<img src=\"$image\" border=\"1\" hspace=\"10\" width=\"50\" align=\"absmiddle\" />"; 
else $string.= "<img src= "<img src=\"$image0\" border=\"1\" hspace=\"10\" width=\"50\" align=\"absmiddle\" />"
Avec cette modification cela devrait pouvoir t'aider :wink:
Une fois ton problème résolu pense à cliquer sur le bouton "Résolu"

Posté : 11 déc. 2008, 12:29
par blof
salut,

une autre solution en utilisant l'opérateur ternaire :
http://fr3.php.net/manual/fr/language.o ... on.ternary
<img src="../personnel/<?php echo is_file('../personnel/'.$tabagent['idagent'].'.jpg') ? $tabagent['idagent'] : '0'; ?>.jpg" ... etc ...
bon, c'est une affaire de goût ...

Posté : 11 déc. 2008, 12:32
par blof
l'ascenseur marche pas ?

Posté : 11 déc. 2008, 12:36
par blof
pour essai :
<?php echo is_file('../personnel/'.$tabagent['idagent'].'.jpg') ? $tabagent['idagent'] : '0'; ?>
désolé de venir faire mes manips perso :oops:

Posté : 11 déc. 2008, 12:55
par Imer2008
A priori ca marche.

Cependant j'ai du faire une gaffe car j'ai ce message d'erreur:
Parse error: syntax error, unexpected ';' in C:\wamp\www\blabla\annuaire\annuaire-new2.php on line 141.
La ligne 141 correspondrait à:
<tr bordercolor="#000000" onMouseOver="this.style.backgroundColor=#FFCCCC" onMouseOut="this.style.backgroundColor=#FFFFFF">

Pouvez-vous m'aider parce que là mes yeux fatiguent lol.
<form name="mailall" method="post" action="<?php echo $PHP_SELF."?validform=ok"; ?>">
                                      <a href="javascript:document.mailall.submit()"></a>
                                      <table align="center" width="80%" border="1" cellpadding="2" cellspacing="1" bordercolor="#000000">
                                        <tr bordercolor="#000000">
											<td width="120" title="Classer la liste suivant le Nom de l'agent"><a href="?ref=nomagent<?php if (!empty($caisse_origine)) { ?>&caisse_origine=<?php echo $caisse_origine; } ?>" class="Style98">Nom</a></td>
											<td width="250" title="Classer la liste suivant le Service de l'agent"><a href="?ref=service<?php if (!empty($caisse_origine)) { ?>&caisse_origine=<?php echo $caisse_origine; } ?>" class="Style98">Service</a></td>
											<td width="120" align="center" title="Classer la liste suivant le Num&eacute;ro de l'agent"><a href="?ref=telephone<?php if (!empty($caisse_origine)) { ?>&caisse_origine=<?php echo $caisse_origine; } ?>" class="Style98">Num&eacute;ro de Poste</a></td>
										</tr>
                                        <?php
										$sql = "SELECT idagent, nomagent,prenomagent,service,fonction,portefeuille,telephone,email FROM agent ";
										if (!empty($caisse_origine)) {
											$sql .= "WHERE caisse_origine = '$caisse_origine' ";
											} 
											$sql .= "ORDER BY $ref";
											$resultat = mysql_query ($sql, $connexion);
											$string = "";
											while ($tabagent = mysql_fetch_array($resultat, MYSQL_ASSOC)) {
												$string .= ?>
										<tr bordercolor="#000000" onMouseOver="this.style.backgroundColor=#FFCCCC" onMouseOut="this.style.backgroundColor=#FFFFFF">										<td><span class="Style95 Style97">
												<?php
												$image = "../personnel/".$tabagent[idagent].".jpg";
												$image0 = "../personnel/image0.jpg";
												if(is_file($image)) $string.=?><img src="<?php $image ?>" border="1" hspace="10" width="50" align="absmiddle" />
												<?php else $string.= ?><img src="<?php $image0 ?>" border="1" hspace="10" width="50" align="absmiddle" />
												<?php $string .= " ".$tabagent[nomagent]." ".$tabagent[prenomagent]?></span></td>
												<?php }
												echo $string;
												?>
											<td><span class="Style95 Style97"><?php echo $tabagent[service]; ?><br>
											<?php if (!empty($tabagent[fonction])) {echo " ~ ".$tabagent[fonction];} ?></span></td>
											<td width="120" align="center"<?php if (empty($tabagent[telephone])) { ?><?php }?>><span class="Style95 Style97">
											<?php $num_interne = "0"; if (strpos($tabagent[telephone], '05010203') !== false or strpos($tabagent[telephone], '05020304') !== false or strpos($tabagent[telephone], '05020304') !== false) { $num_interne = "1";} $telephone = preg_split('//',$tabagent[telephone]); for($i=0;$i<sizeof($telephone);$i++) {echo $telephone[$i]; if ($i%2 == "0") {echo " ";} if ($i == "7" and $num_interne == "1") { echo "<font color='#990000'><b>"; } } ?>
                                            &nbsp;</span>
                                              <span class="Style97">
                                              <?php if ($num_interne == "1") { echo "</b></font>"; }?>
                                          </span></td>
                                        </tr>
                                      </table>
                                  </form>
[/php]

Posté : 11 déc. 2008, 13:36
par yaug
Attention.
si nécessaire n'hésitez pas à aller sur php.net réapprendre les bases :wink:

Ici l'erreur semble cela :
 $string .= ?>
                                        <tr bordercolor="#000000" onMouseOver="this.style.backgroundColor=#FFCCCC" onMouseOut="this.style.backgroundColor=#FFFFFF">                                        <td><span class="Style95 Style97">
                                                <?php
                                                $image 
On ne peut pas faire cela.
$string .= doit etre suivit par quelque chose en php.
Ici ce n'est pas le cas.
Faites quelques chose comme ceci :
$string .= '<tr bordercolor="#000000" onMouseOver="this.style.backgroundColor=#FFCCCC" onMouseOut="this.style.backgroundColor=#FFFFFF">                                        <td><span class="Style95 Style97">';

Posté : 11 déc. 2008, 14:56
par Imer2008
Bon, j'ai recorrigé le code et maintenant j'ai ce problème, mon tableau affiche bien les photos quand il y en a et affiche la photo "no-picture" mais... les deux autres colonnes du tableau passent à l'as...

Je réintègre le code si vous trouvez où est l'erreur.
<table align="center" width="80%" border="1" cellpadding="2" cellspacing="1" bordercolor="#000000">
                                        <tr bordercolor="#000000">
											<td width="120" title="Classer la liste suivant le Nom de l'agent"><a href="?ref=nomagent<?php if (!empty($caisse_origine)) { ?>&caisse_origine=<?php echo $caisse_origine; } ?>" class="Style98">Nom</a></td>
											<td width="250" title="Classer la liste suivant le Service de l'agent"><a href="?ref=service<?php if (!empty($caisse_origine)) { ?>&caisse_origine=<?php echo $caisse_origine; } ?>" class="Style98">Service</a></td>
											<td width="120" align="center" title="Classer la liste suivant le Num&eacute;ro de l'agent"><a href="?ref=telephone<?php if (!empty($caisse_origine)) { ?>&caisse_origine=<?php echo $caisse_origine; } ?>" class="Style98">Num&eacute;ro de Poste</a></td>
										</tr>
                                        <?php 
										$sql = "SELECT idagent, nomagent,prenomagent,service,fonction,portefeuille,telephone,email FROM agent ";
										if (!empty($caisse_origine)) {$sql .= "WHERE caisse_origine = '$caisse_origine' ";}
										$sql .= "ORDER BY $ref";
										$resultat = mysql_query ($sql, $connexion);
										$string = "";
										while ($tabagent = mysql_fetch_array($resultat, MYSQL_ASSOC)) {
											$string .= "<tr bordercolor=\"#000000\" onMouseOver=\"this.style.backgroundColor = '#FFCCCC'\" onMouseOut=\"this.style.backgroundColor = '#FFFFFF'\">
											<td><span class=\"Style95 Style97\">";
											$image = "../personnel/".$tabagent[idagent].".jpg";
											$image0 = "../personnel/image0.jpg";
											if(is_file($image)) $string.= "<img src=\"$image\" border=\"1\" hspace=\"10\" width=\"50\" align=\"absmiddle\" />";
											else $string.= "<img src=\"$image0\" border=\"1\" hspace=\"10\" width=\"50\" align=\"absmiddle\" />"; 
} 
echo $string; 
?>										<td><span class="Style95 Style97"><?php echo $tabagent[service]; ?><br>
											<?php if (!empty($tabagent[fonction])) {echo " ~ ".$tabagent[fonction];} ?></span></td>
											<td width="120" align="center"<?php if (empty($tabagent[telephone])) { ?><?php }?>><span class="Style95 Style97">
											<?php $num_interne = "0"; if (strpos($tabagent[telephone], '05551159') !== false or strpos($tabagent[telephone], '05553397') !== false or strpos($tabagent[telephone], '05553384') !== false) { $num_interne = "1";} $telephone = preg_split('//',$tabagent[telephone]); for($i=0;$i<sizeof($telephone);$i++) {echo $telephone[$i]; if ($i%2 == "0") {echo " ";} if ($i == "7" and $num_interne == "1") { echo "<font color='#990000'><b>"; } } ?>
                                            &nbsp;</span>
                                              <span class="Style97">
                                              <?php if ($num_interne == "1") { echo "</b></font>"; }?>
                                          </span></td>
                                        </tr>
                                      </table>

Et la pièce jointe pour montrer ce que ca donne.
Image

Posté : 11 déc. 2008, 15:09
par yaug
Heuu... normal :)

regarde bien, tu as une boucle.
Ton algorithme est le suivant :

Je rentre dans la boucle
- j'ouvre un tr
- j'ouvre un td
-- j'affiche l'image si il y en a un une
-- j'affiche l'image par défaut sinon
Je ferme ma boucle
Je rajoute mes 2 Td
je ferme mon tr.

C'est plus ou moins ca.

il faut que les "je rajoute mes 2 td" et " je ferme mon tr" soient eux aussi dans la boucle :)

Posté : 11 déc. 2008, 15:21
par blof
ce nest pas :

Code : Tout sélectionner

$tabagent[idagent], $tabagent[service], $tabagent[fonction] ... etc ...
mais :

Code : Tout sélectionner

$tabagent['idagent'], $tabagent['service'], $tabagent['fonction'] ... etc ...
[Note : ce message a été posté de manière anonyme avant d'être réattribué à son auteur]

Posté : 11 déc. 2008, 16:55
par Imer2008
Bon, j'ai quelques soucis à mettre à jour le tableau et à l'afficher correctement mais bon c'est pas grave...