0

I currently have a PHP file that accesses my MySQL database and pulls out the Names and Scores for each player and stores them in an array.

 //This query grabs the top 10 scores, sorting by score and timestamp.
$query = "SELECT * FROM Score ORDER by score DESC, ts ASC LIMIT 10";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

//We find our number of rows
$result_length = mysql_num_rows($result); 

//And now iterate through our results
for($i = 0; $i < $result_length; $i++)
{
     $row = mysql_fetch_array($result);
     echo $row['name'] . "\t" . $row['score'] . "\n"; // And output them
}

What is the best way for me to get both the name and the score from the PHP File and store them in a Javascript Array?

Jayesh Goyani
  • 11,008
  • 11
  • 30
  • 50
Conor Watson
  • 567
  • 1
  • 7
  • 28
  • What's the output of `$row` ? – lshettyl Apr 28 '15 at 10:58
  • As stated below, use an array to return the elements. Have a $results = array(); and then in your for loop append to it with array_push($results, array("name" => $row["name"], "score" => $row["score"])); and at the end return it with json_encode($results); When you get the response on the front end, you can take the response data and JSON.parse() it to turn it into a variable that you can access as parsedVariable[0].name, parsedVariable[0].score, etc. – Taplar Apr 28 '15 at 10:59
  • look here http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript – alquist42 Apr 28 '15 at 11:22
  • I added the following function to my Javascript Code: `//Leaderboard $.get( "HighScores/TopScores.php", function(data) { var results = JSON.parse(data); console.log(2); results.forEach(function(result){ console.log( result.name +" - "+ result.score ); console.log(1) }); }, "json" );` But my Game doesn't seem to print anything out to the Console, not Name, Scores, "1" or "2"? – Conor Watson Apr 28 '15 at 13:09

3 Answers3

1

The best way to store them is store them in json. Use following function

json_encode(arrayname);

and in html use

$.parsejson(responsevariable);

to get original value.

Pratik Gadoya
  • 1,420
  • 1
  • 16
  • 27
0

I would suggest to give json_encoded array to javascript variable (json_encode()), and use javascript json decoding functionality, so you will get what you want :)

Anton
  • 1,029
  • 7
  • 19
0

Create a result variable

$results = array();

Store your results in it in your loop

array_push($results, array("name" => $row["name"], "score" => $row["score"]));

At the end return it with

echo json_encode($results);

When you get the response on the front end, you can take the response data and JSON.parse() it to turn it into a variable that you can access

var results = JSON.parse(data);

results.forEach(function(result){
    console.log( result.name +" - "+ result.score );
});
Taplar
  • 24,788
  • 4
  • 22
  • 35
  • I added the following function to my Javascript Code: `//Leaderboard $.get( "HighScores/TopScores.php", function(data) { var results = JSON.parse(data); console.log(2); results.forEach(function(result){ console.log( result.name +" - "+ result.score ); console.log(1) }); }, "json" );` But my Game doesn't seem to print anything out to the Console, not Name, Scores, "1" or "2"? – Conor Watson Apr 28 '15 at 12:25
  • Try doing a console.log(data) to see what it is coming back as. – Taplar Apr 28 '15 at 13:52
  • I couldn't get the Console to log anything using the $.get function, so I changed it to: `$.ajax({ url: 'HighScores/TopScores.php', type: 'get', success: function(data){ console.log(data); results = JSON.parse(data); console.log(data); LoadedName = results[0].name; LoadedScore = results[0].score; } });` Which outputs: `[{"name":"Conor","score":"100"},{"name":"Mark","score":"100"}]` – Conor Watson Apr 28 '15 at 15:15
  • So that seems to be correct! However now if i use: ` results = JSON.parse(data);` The Chrome console gives me the following error: `Uncaught SyntaxError: Unexpected token <` On Line 1 of my HTML page (Which just loads the Javascript files) – Conor Watson Apr 28 '15 at 15:19
  • It's OK, I managed to fix it! The PHP file was also sending back an error warning me that mysql_connect was deprecated, so once I suppressed the warning then my code worked fine! – Conor Watson Apr 28 '15 at 18:30