Page 1 sur 1
modifier le nom d'une variable JS avec php, possible ?
Posté : 12 nov. 2012, 17:31
par InfoGeo
Bonjour,
Voila je voudrais en fait modifier le code que j ai par un autre plus court et plus utilisable ....
Donc voila j'ai ca:
[javascript]ajax_call (url, function(json){
if (tab_years[k] == "2010") {
if (tab_intOUext[j]== "internal") {
tab_internal2010
= json['ladata']; // tableau contenant le nombre d'internal pour chaque mois suivant l'année et le pays sélectionné
}
else {
tab_external2010 = json['ladata']; // tableau contenant le nombre d'external pour chaque mois suivant l'année et le pays sélectionné
}
}
if (tab_years[k] == "2011") {
if (tab_intOUext[j]== "internal") {
tab_internal2011 = json['ladata'];
}
else {
tab_external2011 = json['ladata'];
}
}
if (tab_years[k] == "2012") {
if (tab_intOUext[j]== "internal") {
tab_internal2012 = json['ladata'];
}
else {
tab_external2012 = json['ladata'];
}
}
if (tab_years[k] == "2013") {
if (tab_intOUext[j]== "internal") {
tab_internal2013 = json['ladata'];
}
else {
tab_external2013 = json['ladata'];
}
}
});[/javascript]
Et pis bon c lourd quoi et en plus il se peut qu'un moment il y ait un année en moins ou en plus .... donc je voudrais faire en sorte d'avoir quelque chose sous cette forme plutot:
if (tab_intOUext[j]== "internal") {
<?php echo "tab_internal";?>tab_years[k][i] = json['ladata'];
}
else {
<?php echo "tab_external";?>tab_years[k][i] = json['ladata'];
}
Enfin voila quoi .... je voudrais qu'il change le nom de la variable suivant l'année dans laquelle on est ... et je sais pas du tout comment faire ...
Donc si vous pourriez m'aider ca serait super sympa =)
a+
Re: modifier le nom d'une variable JS avec php, possible ?
Posté : 12 nov. 2012, 19:45
par Spols
Passer par php est possible mais n'a aucun interet.
Vise plutot une définition dynamique de ta variable, ou mettre l'année en paramètre (voir t'en affranchir complètement) selon l'utilisation que tu fait de cette variable.
http://www.xul.fr/ecmascript/eval.php
Re: modifier le nom d'une variable JS avec php, possible ?
Posté : 12 nov. 2012, 21:59
par AB
Si tu veux faire passer une variable php dans du javascript tu peux faire un simple echo, soit directement dans le code javascript, soit dans un champ html que tu récupéreras par javascript.
Si tu as besoin de faire passer un tableau utilises json_encode côté php.
//javascript
var mon_taleau_js = <?php echo json_encode($mon_tableau_php) ?>;
Re: modifier le nom d'une variable JS avec php, possible ?
Posté : 13 nov. 2012, 10:43
par InfoGeo
@Spols: Merci pour ta réponse, ok je connais pas du tout, je vais essayer de comprendre
@AB: Merci pour ta réponse, mais mon problème ne se situe pas du tout la
Re: modifier le nom d'une variable JS avec php, possible ?
Posté : 13 nov. 2012, 11:08
par InfoGeo
Franchement je sais pas comment faire .......
j'ai essayé comme ca:
[javascript]
ajax_call (url, function(json){
if (tab_intOUext[j]== "internal") {
"tab_internal" + eval(tab_years[k]) + "
" = json['ladata'];
}
else {
"tab_external" + eval(tab_years[k]) + "" = json['ladata'];
}
});
[/javascript]
Mais bon evidemment ca marche pas ... ^^
Re: modifier le nom d'une variable JS avec php, possible ?
Posté : 13 nov. 2012, 12:15
par InfoGeo
ou au pire un truc comme ca:
[javascript]if (tab_intOUext[j]== "internal") {
eval (i + "tab_internal" + tab_years[k])= json['ladata'];
}
else {
eval (i + "tab_external" + tab_years[k])= json['ladata'];
}[/javascript]
.....
Franchement je sais pas trop la ^^
Re: modifier le nom d'une variable JS avec php, possible ?
Posté : 13 nov. 2012, 12:26
par InfoGeo
ou comme ca ?
[javascript]if (tab_intOUext[j]== "internal") {
document.write[i + "tab_internal" + tab_years[k] + "=" + json['ladata']];
}
else {
document.write[i + "tab_external" + tab_years[k] + "=" + json['ladata']];
} [/javascript]
XD
Re: modifier le nom d'une variable JS avec php, possible ?
Posté : 14 nov. 2012, 12:12
par Spols
sans avoir tester oriente toi plutot vers :
[javascript]if (tab_intOUext[j]== "internal") {
eval (i + "tab_internal" + tab_years[k] + "= " + json['ladata']);
}
else {
eval (i + "tab_external" + tab_years[k] + "= " + json['ladata']);
}[/javascript]
.....
Franchement je sais pas trop la ^^[/quote]
Re: modifier le nom d'une variable JS avec php, possible ?
Posté : 14 nov. 2012, 12:15
par InfoGeo
merci pour ta réponse mais bon .... ok sauf que je sais pas quoi faire, ca ca marche pas .
Re: modifier le nom d'une variable JS avec php, possible ?
Posté : 14 nov. 2012, 16:50
par Spols
renseigne toi sur le fonctionnement d'eval
il vaut peut être mieux passer par une variable temporaire
[javascript]if (tab_intOUext[j]== "internal") {
nomVar = i + "tab_internal" + tab_years[k];
}
else {
nomVar = i + "tab_external" + tab_years[k];
}
tempVar = json['ladata'];
eval(nomVar + " = temp" + "Var");
[/javascript]
Sinon je sais pas t'aider ni tester rien qu'avec le bout de script, le plus logique serait de voir comment tu utilise les variables ainsi creer pour apporter une solution optimal
Re: modifier le nom d'une variable JS avec php, possible ?
Posté : 14 nov. 2012, 16:54
par InfoGeo
Merci d avoir voulu m'aider, mais du coup j ai été sur un autre forum et la j ai eu la réponse:
[javascript]window["tab_internal"+tab_years[k].toString()] = json['ladata'];[/javascript]
voila =)
Donc voici mon code:
[javascript]var tab_intOUext = ["internal", "external"];
var tab_years = <?php echo json_encode($tab_years_php); ?>;
<?php
$j=0;
foreach ($tab_years_php as $years) {
echo "tab_internal$tab_years_php[$j] = new Array();
tab_external$tab_years_php[$j] = new Array();";
$j = $j+1;
}
?>
for(var j=0;j<tab_intOUext.length;j++){
for(var k=1;k<tab_years.length;k++){
for(var i=1;i<13;i++){
url = "type_data=Int_Ext&type_areaOUcountry=" + save_type_country + "&nom_data=" + tab_intOUext[j] + "&year=" + tab_years[k] + "&month=" + i + "&areaOUcountry=" + save_country + "&reference=" + selected_reference + "&certification=" + selected_certification + "&trie_intext=" + selected_intext + "&certified=" + selected_certified;
ajax_call (url, function(json){
if (tab_intOUext[j]== "internal") {
window["tab_internal"+tab_years[k].toString()] = json['ladata'];
}
else {
window["tab_external"+tab_years[k].toString()] = json['ladata'];
}
});
}
}
}[/javascript]
Re: [RESOLU] modifier le nom d'une variable JS avec php, pos
Posté : 15 nov. 2012, 01:35
par Spols
Je reste persuadé que ton code est inutilement lourd et anti optimal
Re: [RESOLU] modifier le nom d'une variable JS avec php, pos
Posté : 15 nov. 2012, 19:05
par InfoGeo
Si tu le dis ... en tt cas celui la il marche et vu que je vois pas comment faire autrement et apparemment toi non plus .... alors ca sera ca.
Re: [RESOLU] modifier le nom d'une variable JS avec php, pos
Posté : 16 nov. 2012, 02:15
par Spols
Avec le info que tu nous donne on sait rien faire, et pour repenser la conception complète de ton code il faudrait l'avoir et avoir le temps de le faire.
Mais les pistes à suivre ont été données