IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Trouver un algorithme pour mon problème


Sujet :

Langage PHP

  1. #1
    Membre habitué
    Inscrit en
    Juillet 2006
    Messages
    747
    Détails du profil
    Informations forums :
    Inscription : Juillet 2006
    Messages : 747
    Points : 185
    Points
    185
    Par défaut Trouver un algorithme pour mon problème
    Bonjour à tous

    Je ne sais pas si ce topic est à sa bonne place. Il se trouve que je développe en PHP mais il s'agit surtout d'un problème d'algo sur lequel je bloque (peut être l'heure avancée).

    Voici mon problème :
    Voila mon code (oui, c'est de l'HTML)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    <tr height="160px">
        	<td width="180px"><center><img id="fond_animaux_001" src="images/fond/mini/animaux/01.gif" onclick="selectBackgroundImage('fond_animaux_001');"/></center></td>
         	<td width="180px"><center><img id="fond_animaux_002" src="images/fond/mini/animaux/02.gif" onclick="selectBackgroundImage('fond_animaux_002');"/></center></td>
            <td width="180px"><center><img id="fond_animaux_003" src="images/fond/mini/animaux/03.gif" onclick="selectBackgroundImage('fond_animaux_003');"/></center></td> 
        </tr> 
    	<tr height="160px">
        	<td width="180px"><center><img id="fond_animaux_004" src="images/fond/mini/animaux/04.gif" onclick="selectBackgroundImage('fond_animaux_004');"/></center></td>
         	<td width="180px"><center><img id="fond_animaux_005" src="images/fond/mini/animaux/05.gif" onclick="selectBackgroundImage('fond_animaux_005');"/></center></td>
            <td width="180px"><center><img id="fond_animaux_006" src="images/fond/mini/animaux/06.gif" onclick="selectBackgroundImage('fond_animaux_006');"/></center></td>
        </tr> 
        <tr height="160px">
        	<td width="180px"><center><img id="fond_animaux_007" src="images/fond/mini/animaux/07.gif" onclick="selectBackgroundImage('fond_animaux_007');"/></center></td>
         	<td width="180px"><center><img id="fond_animaux_008" src="images/fond/mini/animaux/08.gif" onclick="selectBackgroundImage('fond_animaux_008');"/></center></td>
            <td width="180px"><center><img id="fond_animaux_009" src="images/fond/mini/animaux/09.gif" onclick="selectBackgroundImage('fond_animaux_009');"/></center></td>
        </tr> 
        <tr height="160px">
        	<td width="180px"><center><img id="fond_animaux_010" src="images/fond/mini/animaux/10.gif" onclick="selectBackgroundImage('fond_animaux_010');"/></center></td>
         	<td width="180px"><center><img id="fond_animaux_011" src="images/fond/mini/animaux/11.gif" onclick="selectBackgroundImage('fond_animaux_011');"/></center></td>
            <td width="180px"><center><img id="fond_animaux_012" src="images/fond/mini/animaux/12.gif" onclick="selectBackgroundImage('fond_animaux_012');"/></center></td>    
        </tr> 
        <tr height="160px">
        	<td width="180px"><center><img id="fond_animaux_013" src="images/fond/mini/animaux/13.gif" onclick="selectBackgroundImage('fond_animaux_013');"/></center></td>
         	<td width="180px"><center><img id="fond_animaux_014" src="images/fond/mini/animaux/14.gif" onclick="selectBackgroundImage('fond_animaux_014');"/></center></td>
            <td width="180px"><center><img id="fond_animaux_015" src="images/fond/mini/animaux/15.gif" onclick="selectBackgroundImage('fond_animaux_015');"/></center></td>
        </tr> 
        <tr height="160px">
        	<td width="180px"><center><img id="fond_animaux_016" src="images/fond/mini/animaux/16.gif" onclick="selectBackgroundImage('fond_animaux_016');"/></center></td>
         	<td width="180px"><center><img id="fond_animaux_017" src="images/fond/mini/animaux/17.gif" onclick="selectBackgroundImage('fond_animaux_017');"/></center></td>
            <td width="180px"><center><img id="fond_animaux_018" src="images/fond/mini/animaux/18.gif" onclick="selectBackgroundImage('fond_animaux_018');"/></center></td> 
        </tr>
    Je voudrais écrire la même chose, en PHP, a l'aide d'une simple boucle en itérant sur le nom du fichier.

    Merci à qui pourra me donner une piste. Je suis complètement bloqué là.

  2. #2
    Expert éminent sénior
    Avatar de rawsrc
    Homme Profil pro
    Dev indep
    Inscrit en
    Mars 2004
    Messages
    6 142
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Dev indep

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 142
    Points : 16 545
    Points
    16 545
    Billets dans le blog
    12
    Par défaut
    Salut,

    si je n'ai pas de boulette cela devrait rouler :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <?php
     
    $html[] = '<tr height="160px">';
     
    while(++$i <= 18) {
       $html[] = '<td width="180px"><center><img id="fond_animaux_' . sprintf('%03d', $i) 
                 . ' src="images/fond/mini/animaux/' . sprintf('%02d', $i) . '.gif"'
                 . ' onclick="selectBackgroundImage(\'fond_animaux_' . sprintf('%03d', $i) . '\');"/></center></td>';
     
       if (($i % 3) == 0) {
          $html[] = '</tr>';
          if ($i < 18) {
             $html[] = '<tr height="160px">';
          }
       }
    }
     
    echo implode("\n", $html);
     
    ?>
    A adapter en fonction du nombre d'éléments bien évidemment

  3. #3
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2011
    Messages
    271
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Italie

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Avril 2011
    Messages : 271
    Points : 491
    Points
    491
    Par défaut
    On pourrait l'écrire comme cela
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <?php
     
    $nb_colonnes=3;
    $nb_fichier=18;
     
    echo "<table border='1'>";
    $x=1;
    while ($x<=$nb_fichier){
    echo '<tr height="160px">';
    	for ($i=0;$i<$nb_colonnes;$i++){
    	echo '<td width="180px"><center>';
    	echo sprintf('<img id="fond_animaux_0%02d" src="images/fond/mini/animaux/%02d.gif" onclick="selectBackgroundImage(this.id);"/>',$x,$x);
    	echo '</center></td>';
    	$x++;
    	}
    echo '</tr>';
    }
    echo "</table>";
    ?>
    J'ai changé le onclick étant donnée que tu passe toujours l'ID de la celllule comme parametre a la fonction selectBackgroundImage

  4. #4
    Membre habitué
    Inscrit en
    Juillet 2006
    Messages
    747
    Détails du profil
    Informations forums :
    Inscription : Juillet 2006
    Messages : 747
    Points : 185
    Points
    185
    Par défaut
    Super Xysyo !!

    Tu m'as bien dépanné. A charge de revanche !

    Edit : fab256, je viens de voir ta méthode. J'aime bien aussi, c'est très propre.

    Merci beaucoup les gars!!

  5. #5
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2011
    Messages
    271
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Italie

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Avril 2011
    Messages : 271
    Points : 491
    Points
    491
    Par défaut
    La solution sera plus courte avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    onclick="selectBackgroundImage(this.id);"
    et ca sera encore mieux si tu utilise les feuilles de style CSS pour formater ton tableau.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. trouver des icones pour mon application
    Par rudhf dans le forum Général Dotnet
    Réponses: 14
    Dernier message: 25/02/2010, 12h15
  2. Recherche Equation ou Algorithme de mon Problème
    Par Quintoux dans le forum Mathématiques
    Réponses: 3
    Dernier message: 05/11/2009, 17h37
  3. Goto pour mon problème est-il la solution ?
    Par Beginer dans le forum C++
    Réponses: 9
    Dernier message: 22/01/2009, 08h58
  4. [Exercice] Trouver l'algorithme pour cet exercice.
    Par zinedineilham dans le forum Algorithmes et structures de données
    Réponses: 1
    Dernier message: 23/11/2008, 00h13

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo