Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Incrementer le nom d'une variable

3 réponses
Avatar
Steph
Bonjour,

Je souhaite pouvoir incrémenter le nom d'une variable afin de pouvoir faire
une boucle.


J'ai essayer ceci mais ca ne fonctionne pas, avez vous une idée ?
--------------------------------
while ($a = 22) {
++$a;
$nl = $n['$a'];
$hl = $h['$a'];
echo "$nl";
if ($nl == 0) {echo"<td nowrap><font color='$color1'><center>$hl</td>";}
if ($nl == 1) {echo"<td nowrap><font color='$color4'><center>$hl</td>";}
if ($nl == 2) {echo"<td nowrap><font color='$color3'><center>$hl</td>";}
if ($nl == 3) {echo"<td nowrap><font color='$color2'><center>$hl</td>";}
if ($nl == 4) {echo"<td nowrap><font color='$color1'><center>$h1</td>";}
}
-----------------------------------

D'avance merci pour votre aide,

Steph

3 réponses

Avatar
Olivier Miakinen

J'ai essayer ceci mais ca ne fonctionne pas, avez vous une idée ?
--------------------------------
while ($a = 22) {


Ici tu affectes la valeur 22 à la variable $a. Tu viens donc de réaliser
une boucle infinie, dans laquelle $a vaudra 22 à chaque tour de boucle.

++$a;


Ici, $a vaudra 23 à chaque tour de boucle.

$nl = $n['$a'];
$hl = $h['$a'];


Ici, à supposer que tes tableaux ont des valeurs indexées pas la chaîne
'$a' (et non pas '23'), ce sont ces valeurs qui seront retournées. S'il
y a un index '23', il faudrait plutôt "$a" (guillemets doubles).

echo "$nl";
if ($nl == 0) {echo"<td nowrap><font color='$color1'><center>$hl</td>";}


Vraisemblablement, $nl sera vide, et la comparaison avec 0 devrait
réussir. Tu devrais donc écrire une infinité de lignes comme celle-ci.

if ($nl == 1) {echo"<td nowrap><font color='$color4'><center>$hl</td>";}
if ($nl == 2) {echo"<td nowrap><font color='$color3'><center>$hl</td>";}
if ($nl == 3) {echo"<td nowrap><font color='$color2'><center>$hl</td>";}
if ($nl == 4) {echo"<td nowrap><font color='$color1'><center>$h1</td>";}


Tu connais l'instruction switch ?

D'avance merci pour votre aide,


À lire de toute urgence :
http://www.php.net/manual/fr/

Avatar
Pascal
Le Fri, 08 Sep 2006 21:01:33 +0000, Steph a écrit :

Bonjour,

Je souhaite pouvoir incrémenter le nom d'une variable afin de pouvoir
faire une boucle.


Pas bien :)



J'ai essayer ceci mais ca ne fonctionne pas, avez vous une idée ?


Oui utiliser un tableau, c'est fait pour

--------------------------------
while ($a = 22) {


La va y avoir un problème, time out assuré ou dépassement mémoire ou
plantage au choix... C'est pas while($a=") ou while($a!") ?

++$a;
$nl = $n['$a'];
$hl = $h['$a'];
echo "$nl";
if ($nl == 0) {echo"<td nowrap><font color='$color1'><center>$hl</td>";}
if ($nl == 1) {echo"<td nowrap><font color='$color4'><center>$hl</td>";}
if ($nl == 2) {echo"<td nowrap><font color='$color3'><center>$hl</td>";}
if ($nl == 3) {echo"<td nowrap><font color='$color2'><center>$hl</td>";}
if ($nl == 4) {echo"<td nowrap><font color='$color1'><center>$h1</td>";}
}


utiliser $color[1], $color[2]... ?

-----------------------------------

D'avance merci pour votre aide,



De rien,
Pascal

Avatar
Bruno Desthuilliers
(snip)
if ($nl == 0) {echo"<td nowrap><font color='$color1'><center>$hl</td>";}
if ($nl == 1) {echo"<td nowrap><font color='$color4'><center>$hl</td>";}
if ($nl == 2) {echo"<td nowrap><font color='$color3'><center>$hl</td>";}
if ($nl == 3) {echo"<td nowrap><font color='$color2'><center>$hl</td>";}
if ($nl == 4) {echo"<td nowrap><font color='$color1'><center>$h1</td>";}



Tu connais l'instruction switch ?


Pourquoi faire ?

$maxcolor = 5;
echo "<td nowrap><font color='"
. ${"color" . ($nl == 0 ? 1 : $maxcolor - $nl)}
. "><center>$h1</td>";

!-)

Mais bon, la simple énumération color1, color2, colorN - tout comme le
sujet du post d'ailleur - suffit à savoir comment écrire ça lisiblement:

$colors = Array("couleur1", "couleur2", ... , "couleurN");
$color_index = $nl == 0 ? 1 : size($colors) - $nl;
echo"<td nowrap><font color='{$colors[$color_index]}'><center>$h1</td>";

Et accessoirement, envisager de se simplifier la vie en mettant les
choses dans le bon ordre:

$colors = Array("couleurN", ..., "couleur2", "couleur1");
echo"<td nowrap><font color='{$colors[$nl]}'><center>$h1</td>";


Mes deux centimes...