-1

I want to use this array

while($row = mysqli_fetch_array($result)) {
      $outlet_name[] = $row['outlet_name']; 
    }

into a variable like this

var availableTags = [
    "ActionScript",
    "AppleScript",
    "Asp",
    "BASIC",
    "C",
    "C++",
    "Clojure",
    "COBOL"
];

I'm not sure of how to best do that? I'm new to all of this so if you please give me some help I'm very appreciate

3 Answers3

1

json_encode the array after your loop, and output the JSON:

<?php
while($row = mysqli_fetch_array($result)) {
    $outlet_name[] = $row['outlet_name']; 
}
?>
<script>
    var availableTags = <?= json_encode($outlet_name) ?>;
</script>
<?php
...
Paul
  • 139,544
  • 27
  • 275
  • 264
0

Try below:

var availableTags = [
<?php foreach($outlet_name as $name){
    echo $name.",";
}?>
];

Hope this helps..

Rahul Kaushik
  • 1,454
  • 8
  • 16
0
while($row = mysqli_fetch_array($result)) {
  $outlet_name[] = $row['outlet_name']; 
}    

echo 'var availableTags = ' . json_encode($outlet_name) . ';';
Nikita U.
  • 3,540
  • 1
  • 28
  • 37