I am getting ~ 2k cities name from my Database that I am storing in a session variable, then I am trying to convert my session variable into a javascript variable but my code seem to crash
Code for session variable
$sql = "SELECT nom FROM Villes ORDER BY nbSearch";
try
{ $resultat = $bd->Select($sql); }
catch(Exception $e)
{ echo $e->getMessage(); }
if(count($resultat) != 0)
$_SESSION['villeDB'] = $resultat;
echo count($resultat)."<br/>";
php to javascript
var availableTags = '<?php echo json_encode($_SESSION['villeDB']) ?>';
I need to store each city name into that kind of JavaScript var
var availableTags = [];
You could try deleting quotes:
var availableTags = <?php echo json_encode($_SESSION['villeDB']) ?>;
If you want to convert [["Acme, AB"],["Airdrie, AB"],...]
to ["Acme, AB","Airdrie, AB",...]
, you can use something like
var temp = [];
for (var i=0; i<availableTags.length; ++i) {
for (var j=0; j<availableTags[i].length; ++j) {
temp.push( availableTags[i][j] );
}
}
availableTags = temp;
but if you ask it in another question maybe someone will answer a better option.