Précédent   Forum des professionnels en informatique > PHP > PHP & SGBD > PHP & MySQL
PHP & MySQL Forum d'entraide sur les fonctions MySQL avec PHP. Avant de poster -> FAQ MySQL, Cours MySQL et Sources MySQL. Pour les questions concernant le moteur MySQL plutôt que les fonctions PHP, merci d'utiliser le forum MySQL.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 08/12/2011, 10h52   #1
Membre actif
 
Homme
Inscription : avril 2011
Messages : 426
Détails du profil
Informations personnelles :
Sexe : Homme

Informations forums :
Inscription : avril 2011
Messages : 426
Points : 171
Points : 171
Par défaut Création d'une variable variable

Bonjour,

J'aimerais votre aide pour créer une variable variable.

En effet, j'aimerais faire changer une image selon cette variable. Voici ce que j'ai essayé mais qui ne fonctionne pas.

La variable "$checked_image" doit donc devenir '$checked_image_1" ou "_1" est variable selon l'identifiant reçu par "$_GET['id_sous_type_article']".


Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        if(!empty($_GET['id_sous_type_article']))
		{
			$debut ='_checked';
			$suite = intval($_GET['id_sous_type_article']);
			$suite ='_'.$suite; //ex : _1
 
			$checked_image = ${$debut.'_'.$suite};
 
		}
 
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=1" title="Vélo"><img src="/php/images/mountain_bike'.$checked_image_1.'.png" border="0" alt="Icône vélo"></a></p>';
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=2" title="Randonnée"><img src="/php/images/hikking'.$checked_image_2.'.png" border="0" alt="Icône randonneurs"></a></p>';
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=3" title="Sécurité et cartographie"><img src="/php/images/safety'.$checked_image_3.'.png" border="0" alt="Icône croix secours"></a></p>';
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=4" title="Vêtements"><img src="/php/images/cloth'.$checked_image_3.'.png" border="0" alt="Icône ceintre vêtement"></a></p>';
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=5" title="Bivouac"><img src="/php/images/sleep'.$checked_image_4.'.png" border="0" alt=""></a></p>';
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=6" title="Autre"><img src="/php/images/other'.$checked_image_5.'.png" border="0" alt="Icône point d\'interrogation"></a></p>';

par avance, merci pour le coup de main
heretik25 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/12/2011, 11h26   #2
Expert Confirmé
 
Avatar de rawsrc
 
Homme Martin
Dev indep
Inscription : mars 2004
Messages : 1 462
Détails du profil
Informations personnelles :
Nom : Homme Martin
Âge : 35
Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

Informations professionnelles :
Activité : Dev indep

Informations forums :
Inscription : mars 2004
Messages : 1 462
Points : 2 552
Points : 2 552
Envoyer un message via Skype™ à rawsrc
Bonjour,
Citation:
Envoyé par heretik25 Voir le message
La variable "$checked_image" doit donc devenir '$checked_image_1" ou "_1" est variable selon l'identifiant reçu par "$_GET['id_sous_type_article']".
Je pense qu'il y a un os dans la conception, une variable ne devient pas une autre variable, tu créés directement la nouvelle variable.
__________________
# Dans la Création, tout est permis mais tout n'est pas utile...
rawsrc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/12/2011, 11h28   #3
Membre actif
 
Homme
Inscription : avril 2011
Messages : 426
Détails du profil
Informations personnelles :
Sexe : Homme

Informations forums :
Inscription : avril 2011
Messages : 426
Points : 171
Points : 171
Dans ce cas,

comment créer une variable avec un nom différent selon une autre variable ?

L'objectif est de changer uniquement l'image concernée par la variable $_GET[id_sous_type_article'];
heretik25 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/12/2011, 11h32   #4
Expert Confirmé
 
Avatar de rawsrc
 
Homme Martin
Dev indep
Inscription : mars 2004
Messages : 1 462
Détails du profil
Informations personnelles :
Nom : Homme Martin
Âge : 35
Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

Informations professionnelles :
Activité : Dev indep

Informations forums :
Inscription : mars 2004
Messages : 1 462
Points : 2 552
Points : 2 552
Envoyer un message via Skype™ à rawsrc
Ne serait-ce pas ça que tu recherches ?
Code :
1
2
3
4
5
6
7
<?php
 
$num = '_1';
$a   = 'checked_image'. $num;
$b   = $$a; // php va rechercher une variable $checked_image_1
 
?>
Un lien avec quelques explications : variables en php
__________________
# Dans la Création, tout est permis mais tout n'est pas utile...
rawsrc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/12/2011, 11h38   #5
Membre actif
 
Homme
Inscription : avril 2011
Messages : 426
Détails du profil
Informations personnelles :
Sexe : Homme

Informations forums :
Inscription : avril 2011
Messages : 426
Points : 171
Points : 171
Si si , c'est bien ça !

par contre, comment lui affecter la valeur "checked" ?
heretik25 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/12/2011, 11h44   #6
Expert Confirmé
 
Avatar de rawsrc
 
Homme Martin
Dev indep
Inscription : mars 2004
Messages : 1 462
Détails du profil
Informations personnelles :
Nom : Homme Martin
Âge : 35
Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

Informations professionnelles :
Activité : Dev indep

Informations forums :
Inscription : mars 2004
Messages : 1 462
Points : 2 552
Points : 2 552
Envoyer un message via Skype™ à rawsrc
Bah, faut bouquiner un peu les liens qu'on file, c'est utile tu sais
Code :
1
2
3
4
5
6
7
<?php
 
$num = '_1';
$a   = 'checked_image'. $num;
$$a  = '"checked"';
 
?>
C'était expliqué dès le 1er exemple du lien que je t'avais filé, mais bon...
__________________
# Dans la Création, tout est permis mais tout n'est pas utile...
rawsrc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/12/2011, 11h47   #7
Membre actif
 
Homme
Inscription : avril 2011
Messages : 426
Détails du profil
Informations personnelles :
Sexe : Homme

Informations forums :
Inscription : avril 2011
Messages : 426
Points : 171
Points : 171
Pardon, je n'avais pas fait attention au lien


EDIT : Marche impecable


Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
if(!empty($_GET['id_sous_type_article']))
		{			
			$num = '_'.intval($_GET['id_sous_type_article']).'';
			$a   = 'checked_image'. $num;
			$$a  = '_checked';
		}
 
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=1" title="Vélo"><img src="/php/images/mountain_bike'.$checked_image_1.'.png" border="0" alt="Icône vélo"></a></p>';
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=2" title="Randonnée"><img src="/php/images/hikking'.$checked_image_2.'.png" border="0" alt="Icône randonneurs"></a></p>';
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=3" title="Sécurité et cartographie"><img src="/php/images/safety'.$checked_image_3.'.png" border="0" alt="Icône croix secours"></a></p>';
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=4" title="Vêtements"><img src="/php/images/cloth'.$checked_image_4.'.png" border="0" alt="Icône ceintre vêtement"></a></p>';
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=5" title="Bivouac"><img src="/php/images/sleep'.$checked_image_5.'.png" border="0" alt=""></a></p>';
		echo'<p class="flottante2"><a href="/php/articles/index.php?id_type_article='.$id_type_article.'&amp;id_sous_type_article=6" title="Autre"><img src="/php/images/other'.$checked_image_6.'.png" border="0" alt="Icône point d\'interrogation"></a></p>';
Un grand merci pour le coup de mains !
heretik25 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 18h43.


 
 
 
 
Partenaires

Hébergement Web