How can I populate a javascript array with values ​​from a database using PHP?

I need to create a javascript array whose elements are retrieved by php from the database. Is it possible? If so, how?

(I don't want to use ajax for this)

+2


source to share


5 answers


Collect the values ​​in an array and convert them to JSON with json_encode

:



$array = array();
while ($row = mysql_fetch_assoc($result)) {
    $array[] = $row['key'];
}
echo 'var array = '.json_encode($array).';';

      

+5


source


Answer 1: yes, it can be done.

Answer 2: Here's how:



$js_array = "[";
$result = mysql_query("some query of yours");
while( $row=mysql_fetch_array($result, MYSQL_NUM) ) {
    $js_array .= $row[0]; // assuming you just want the first field 
                          // of each row in the array
    $js_array .= ",";
}
$js_array{ strlen($js_array)-1 } = ']';

echo "var db_array = $js_array ;";

      

Hooray!

+4


source


If you can loop through the contents of the database, creating an array is simple:

echo "var myArray = [";

      

Scroll through the array here and add commas to separate values.

echo "];";

      

Your myArray variable will be available client side. It should look something like this:

var myArray = [1, 3, 2, 4];

      

Alternatively and to eliminate the extra comma due to loop-based concatenation, you can write a variable to the client like this:

echo "var myArray = [];"

      

... and do the following for each line of data:

echo "myArray.push($db_data);"

      

This is effective, although not as clean as the previous approach. You will receive code similar to the following:

var myArray = [];

myArray.push(3);
myArray.push(5);
myArray.push(1);

alert(myArray); // will display "3, 5, 1"

      

My apologies for not being too familiar with PHP, but this is something that can be done in any server-side language.

+1


source


<script language="javascript1.2">
var arr = new array(<?php 
$result = mysql_query("select .... ");
$count = mysql_num_rows($result);
if($count>0){
  for( $i=0;$i<$count-1;$i++) {
    $row = mysql_fetch_row($result);
    echo $row[0],',';
  }
  $row = mysql_fetch_row($result);
  echo $row[0],',';
}
?>);
</script>

      

+1


source


Here is a solution that correctly deals with single quotes in data:

First, fetch the database data, for example using the mysqli_ functions:

$result = mysqli_query($con, $sql);
if (!$result) die(); // error handling comes here
$array = mysqli_fetch_all($result, MYSQLI_ASSOC);

      

Then convert it to JSON, escape single quotes and output JavaScript:

$output = str_replace("'", "\'", json_encode($array));
echo "var array = '$output';";

      

Note that it json_encode

speeds up the backslashes (as it should) by doubling them. And it's okay that it doesn't escape single quotes since it can't know the context we're putting it in. So when we embed it in JavaScript by wrapping it in single quotes, we are also responsible for excluding the quotes.

0


source







All Articles