Modifier un script

ensemblevide
Invité n'ayant pas de compte PHPfrance

21 oct. 2005, 11:08

Bonjour

J'ai un petit script qui me permet d'afficher dans une page une image, un texte et une url provenant d'une BDD.
Ca affiche ces infos par rapport à l'id_article de la page en cours:

Code : Tout sélectionner

// ON RECUPERE LES DONNEES CORRESPONDANTES A L'UTILISATEUR LOGE $select_db=@mysql_select_db($sql_bdd); if($select_db){ $result = mysql_query("SELECT * FROM affiliation WHERE id_article=#ID_ARTICLE",$db_link) or die(mysql_error()); if (mysql_num_rows($result) == 0) { echo " "; }else{ $row = mysql_fetch_assoc($result);?>
Pour info #ID_ARTICLE est spécifique à SPIP. Puis j'affiche :

Code : Tout sélectionner

<tr> <td width="60" rowspan="2"><a target="blank" href="<?echo $row['url1'];?>"><img src="IMG/<?echo $row['image'];?>" width="60" height="55" border="0"></a></td> <td width="2"><img src="IMG/pixel_transparent.gif" width="2" height="2"></td> <td width="116" class="textegrastitre"><?echo $row['artiste'];?> - <?echo $row['titre'];?></td> </tr>
Ca ne fonctionne qu'avec un enregistrement provenant de la base.
Maintenant je voudrais avoir la possibilité d'afficher plusieurs enregistrements si ils ont tous le même id_article.
Comment faire ? Je débute en php
Merci pour votre aide
Philippe

Eléphant du PHP | 153 Messages

21 oct. 2005, 11:42

Tu pourrais par exemple faire

Code : Tout sélectionner

<table>

while($row = mysql_fetch_assoc($result)){

Ce qui se traduit par tant qu'il y a des enregistrements correspondants a ma requête dans la base

Code : Tout sélectionner

<tr> <td width="60" rowspan="2"><a target="blank" href="<?echo $row['url1'];?>"><img src="IMG/<?echo $row['image'];?>" width="60" height="55" border="0"></a></td> <td width="2"><img src="IMG/pixel_transparent.gif" width="2" height="2"></td> <td width="116" class="textegrastitre"><?echo $row['artiste'];?> - <?echo $row['titre'];?></td> </tr>
}

Code : Tout sélectionner

</table>
La tu commence par placer ta balise de tableau puis à chaque enregistrement tu construis une ligne et enfin quand il n'y en a plus tu refermes ton tableau.

ensemblevide
Invité n'ayant pas de compte PHPfrance

21 oct. 2005, 12:13

Merci pour ta réponse.
Est-ce que la consctruction du tableau se fait automatiquement ? Parce que apriori sur certaines pages il peut y avoir un enregistrement et sur d'autres plusieurs.

ensemblevide
Invité n'ayant pas de compte PHPfrance

21 oct. 2005, 12:25

J'ai oublié un bout de code, ça pourrait faire changer ta proposition ?

Code : Tout sélectionner

// ON RECUPERE LES DONNEES CORRESPONDANTES A L'UTILISATEUR LOGE $select_db=@mysql_select_db($sql_bdd); if($select_db){ $result = mysql_query("SELECT * FROM affiliation WHERE id_article=#ID_ARTICLE",$db_link) or die(mysql_error()); if (mysql_num_rows($result) == 0) { echo "&nbsp;"; }else{ $row = mysql_fetch_assoc($result);?><table width="179" border="0" cellspacing="0" cellpadding="0"> <tr> <td colspan="3"><img src="IMG/commandez-#LANG.gif" width="178" height="15"></td> </tr> <tr> <td colspan="3"><img src="IMG/pixel_transparent.gif" width="178" height="2"></td> </tr> <tr> <td width="60" rowspan="2"><a target="blank" href="<?echo $row['url1'];?>"><img src="IMG/<?echo $row['image'];?>" width="60" height="55" border="0"></a></td> <td width="2"><img src="IMG/pixel_transparent.gif" width="2" height="2"></td> <td width="116" class="textegrastitre"><?echo $row['artiste'];?> - <?echo $row['titre'];?></td> </tr> <tr> <td width="3"><img src="IMG/pixel_transparent.gif" width="2" height="2"></td> <td><a target="blank" class="liensorange" href="<?echo $row['url1'];?>"><:commandez:></a></td> </tr> </table><? } }?>
La partie :

Code : Tout sélectionner

<tr> <td colspan="3"><img src="IMG/commandez-#LANG.gif" width="178" height="15"></td> </tr> <tr> <td colspan="3"><img src="IMG/pixel_transparent.gif" width="178" height="2"></td> </tr>
ne se duplique pas

Désolé :?

Eléphant du PHP | 153 Messages

21 oct. 2005, 15:01

elle ne se duplique pas c'est à dire?Normalement il n'y a aucun problème

Sinon ce que tu as oublié ne change rien au code que je t'ai proposé tu dois juste placer tes balises <table> et </table en dehors de la boucle while pour n'avoir qu'un seul tableau enfin c'est pas obligé tout dépend de tes besoins

Invité
Invité n'ayant pas de compte PHPfrance

21 oct. 2005, 18:35

je voulais dire q'une partie ne devait pas être dans la boucle while
En tout cas merci je m'en suis sorti avec :

Code : Tout sélectionner

// ON RECUPERE LES DONNEES CORRESPONDANTES A L'UTILISATEUR LOGE $select_db=@mysql_select_db($sql_bdd); if($select_db){ $result = mysql_query("SELECT * FROM affiliation WHERE id_article=#ID_ARTICLE",$db_link) or die(mysql_error()); if (mysql_num_rows($result) == 0) { echo "&nbsp;"; }else{?> <table width="179" border="0" cellspacing="0" cellpadding="0"> <tr> <td colspan="3"><img src="IMG/commandez-#LANG.gif" width="178" height="15"></td> </tr> <tr> <td colspan="3"><img src="IMG/pixel_transparent.gif" width="178" height="2"></td> </tr> <tr> <table> <? while($row = mysql_fetch_assoc($result)){?> <tr> <td width="60" rowspan="2"><a target="blank" href="<?echo $row['url1'];?>"><img src="IMG/<?echo $row['image'];?>" width="60" height="55" border="0"></a></td> <td width="2"><img src="IMG/pixel_transparent.gif" width="2" height="2"></td> <td width="116" class="textegrastitre"><?echo $row['artiste'];?> - <?echo $row['titre'];?></td> </tr> <tr> <td width="3"><img src="IMG/pixel_transparent.gif" width="2" height="2"></td> <td><a target="blank" class="liensorange" href="<?echo $row['url1'];?>"><:commandez:></a></td> </tr> <?}?> <table> </tr> </table><? } }?>