Reputation: 55
how can i escape this : var tab_mois_nb_match = <?php ".json_encode($tab_mois_nb_match)." ;?> ;
I have an code error, but the arrays are generate with sussess in the conle.log, it's totally crazy.
foreach($tab_bases2 as $key => $univers){
$tab_nb_match_par_user = users_nb_match($univers);
$tab_mois_nb_match = mois_nb_match($univers);
echo "<div id='".$univers."' ></div>";
echo "<script type='text/JavaScript'>
var tab_mois_nb_match = <?php ".json_encode($tab_mois_nb_match)." ;?> ;
var line3 = [];tab_date = [];
for(var v in tab_mois_nb_match){
line3.push([v,Number(tab_mois_nb_match[v])]);
tab_date.push(v[0]+v[1]+v[2]+v[3]);
}
console.log(tab_mois_nb_match);
</script>";
}
Upvotes: 0
Views: 78
Reputation: 33502
You are mixing inline PHP with a PHP command (echo).
When you are echoing a string, you do it just like normal, this means you can mix literal strings (the js you are manually typing) and the output of functions (like a json in this case):
echo "<script type='text/JavaScript'>
var tab_mois_nb_match = ".json_encode($tab_mois_nb_match)." ;
var line3 = [];tab_date = [];
for(var v in tab_mois_nb_match){
line3.push([v,Number(tab_mois_nb_match[v])]);
tab_date.push(v[0]+v[1]+v[2]+v[3]);
}
console.log(tab_mois_nb_match);
</script>";
A string is a string.
Edit: On that note though, I should add, if you need something to be evaluated PRIOR to being inserted into a string, just drop some brackets around it.
echo "<script type='text/JavaScript'>
var tab_mois_nb_match = ".(json_encode($tab_mois_nb_match)+4)." ;
var line3 = [];tab_date = [];
for(var v in tab_mois_nb_match){
line3.push([v,Number(tab_mois_nb_match[v])]);
tab_date.push(v[0]+v[1]+v[2]+v[3]);
}
console.log(tab_mois_nb_match);
</script>";
Okay, so +4 is a bad example, but say for example, you needed the output of a function inserted into another function... brackets are the ones that do the trick.
Lastly, not ALL functions will work like this. Sometimes, you just have to save the output of a function into a variable and then insert the variable into the string.
Upvotes: 2
Reputation: 1590
You cannot declare php inside php.
You can write your code in two ways:
First Method:
foreach($tab_bases2 as $key => $univers){
$tab_nb_match_par_user = users_nb_match($univers);
$tab_mois_nb_match = mois_nb_match($univers);
echo "<div id='".$univers."' ></div>";
?>
<script type='text/JavaScript'>
var tab_mois_nb_match = <?php json_encode($tab_mois_nb_match) ;?> ;
var line3 = [];tab_date = [];
for(var v in tab_mois_nb_match){
line3.push([v,Number(tab_mois_nb_match[v])]);
tab_date.push(v[0]+v[1]+v[2]+v[3]);
}
console.log(tab_mois_nb_match);
</script>
<?php
}
?>
Second Method
foreach($tab_bases2 as $key => $univers){
$tab_nb_match_par_user = users_nb_match($univers);
$tab_mois_nb_match = mois_nb_match($univers);
echo "<div id='".$univers."' ></div>";
echo "<script type='text/JavaScript'>
var tab_mois_nb_match = ".json_encode($tab_mois_nb_match)." ;
var line3 = [];tab_date = [];
for(var v in tab_mois_nb_match){
line3.push([v,Number(tab_mois_nb_match[v])]);
tab_date.push(v[0]+v[1]+v[2]+v[3]);
}
console.log(tab_mois_nb_match);
</script>";
}
Upvotes: 2