Brad M
Brad M

Reputation: 17

Getting mysql data into a var array

Im using MySQL to obtain data into $username and $chance.

There are two usernames in the data but it only loads the first one.

var data = [
    {
         "name" : "<?php echo $username; ?>", 
         "hvalue" : <?php echo $chance; ?> 
    },
];

Upvotes: 0

Views: 56

Answers (1)

akrys
akrys

Reputation: 563

To give a correct answer, we'll have to know what your variables $username and $chance exactly look like.

  1. If you only have a string there, then only one value is possible.
  2. If these variables are php arrays, you'll have to use json_encode to make a JSON array from them, before you can add them to your javascript object. (see https://secure.php.net/manual/en/function.json-encode.php)

Anyway, the best way to send a PHP array to JS would be to create the whole array in PHP and then encode it to JSON in order to use all the values in JS.

var data = <?php echo json_encode($data); ?>

More advanced: use an ajax request to avoid mixing up PHP an JS. But that's another story.

Upvotes: 1

Related Questions