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.2k11 gold badges32 silver badges50 bronze badges
asked Apr 28, 2015 at 10:49
4
  • What's the output of $row ? Commented Apr 28, 2015 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. Commented Apr 28, 2015 at 10:59
  • look here stackoverflow.com/questions/23740548/… Commented Apr 28, 2015 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"? Commented Apr 28, 2015 at 13:09

3 Answers 3

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.

answered Apr 28, 2015 at 10:52
Sign up to request clarification or add additional context in comments.

Comments

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 :)

answered Apr 28, 2015 at 10:51

Comments

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 );
});
answered Apr 28, 2015 at 11:02

5 Comments

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"?
Try doing a console.log(data) to see what it is coming back as.
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"}]
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)
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!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.