|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
arnogb69 Smarty Rookie
Joined: 07 May 2008 Posts: 11
|
Posted: Wed May 07, 2008 1:10 pm Post subject: Forcer l'incrementation d'une boucle |
|
|
Bonjour à tous,
J'ai un petit soucis qui me prend la tete depuis pas mal de temps :
J'aimerai savoir s'il est possible d'incrementer un curseur "manuellement" dans une section (ou un foreach) : je m'explique :
Je souhaiterai arriver à un résultat final comme ceci :
Quote: | Pas de fichier 1 : enregistrer un fichier
Fichier 2 : nom2
Pas de fichier 3 : enregistrer un fichier
Fichier 4 : nom4
Pas de fichier 5 : enregistrer un fichier |
En résumé, si j'ai dans ma base un fichier dont l'id est 2 et un autre dont l'id est 4, j'affiche leurs noms. Sinon, j'offre la possibilité d'un ajouter un.
Voici le code que j'ai prodit :
Code: | {section name=test2 loop=5}
passage {$smarty.section.test2.iteration} : id = {$fichier2[test2].id}<br>
{if $fichier2[test2].id == $smarty.section.test2.iteration}
Fichier {$fichier2[test2].id} : {$fichier2[test2].nom} <br/>
{else}
Pas de fichier {$smarty.section.test2.iteration} : enregistrer un fichier <br/>
{/if}
{/section} |
Le problème vient du fait que l'incrementation de mon curseur va me comparer l'id de mon premier fichier (donc 2) avec le numéro de l iteration de la boucle (donc 1), puis incrementer à son tour pour comparer mon deuxieme fichier (id=4) avec la deuxieme iteration (1).
Ce qui va m'afficher :
Quote: | passage 1 : id = 2
Pas de fichier 1 : enregistrer un fichier
passage 2 : id = 4
Pas de fichier 2 : enregistrer un fichier
passage 3 : id =
Pas de fichier 3 : enregistrer un fichier
passage 4 : id =
Pas de fichier 4 : enregistrer un fichier
passage 5 : id =
Pas de fichier 5 : enregistrer un fichier |
Les id ne correspondant jamais, je ne rentre jamais dans mon {if}
En fait, mon algo est simple mais je ne sais pas comment l implementer sous Smarty :
Code: |
{boucle Smarty loop=5}
{if numeroID == numIteration }
Affichage du nom
[b]ITERATION DE MON CURSEUR[/b]
{else}
Affichage Ajout
[b]PAS D ITERATION[/b]
{fin de boucle}
COmment gérer ces deux lignes en gras, itération ou non?
Merci d'avance |
|
|
Back to top |
|
arnogb69 Smarty Rookie
Joined: 07 May 2008 Posts: 11
|
Posted: Wed May 07, 2008 2:02 pm Post subject: |
|
|
Pfiou, c'était assez logique en fait, il suffisait d'ajouter un compteur et de se ballader manuellement dans mon tableau
Pour ceux que cela intéresse, voici la soluce :
Code: | <!-- Initialisation du compteur -->
{counter start=0 assign=compteur}
<!-- on boucle "loop" fois -->
{section name=test2 loop=5}
<!-- si on a un fichier dans la base, on l affiche puis on incremente le compteur-->
{if $fichier2[$compteur].id == $smarty.section.test2.iteration}
Fichier {$fichier2[$compteur].id} : {$fichier2[$compteur].nom} <br/>
{counter}
<!-- sinon, on propose d enregistrer un fichier -->
{else}
Pas de fichier {$smarty.section.test2.iteration} : enregistrer un fichier <br/>
{/if}
{/section} |
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|