0

I´m trying to create my own leaderboards sytem for my games so I´m working with PHP and requesting info with Ajax into the games, but as I´m not good at all with PHP I´m pretty confused about how to create a JSON object with all the info I need to handle in the javascript part.

What I want to do, in the PHP part is to generate this JSON object:

{players: ["name1", "name2", ..., "name10"], scores:[score1, score2, ..., score10]}

So I can work in javascript with something like

dataReceived.players[0]

I´m storing and getting the data correctly from the database but I´m not being able to generate that JSON object to receive in the Ajax request. Basically, this is my PHP code:

$query = "SELECT * FROM leadersboards ORDER by score ASC LIMIT 10";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

$result_length = mysql_num_rows($result);

$arrayScores = array();
$arrayPlayers = array();

for($i = 0; $i < $result_length; $i++){
    $row = mysql_fetch_array($result);
    array_push($arrayPlayers, $row['player']);
    array_push($arrayScores, $row['score']);
}
$answer = json_encode(array('item' => $arrayPlayers, 'item' => $arrayScores), JSON_FORCE_OBJECT);

Sorry if I made something stupid in PHP, as I said, I don´t know PHP at all, just pretty basic stuff.

n-dru
  • 9,285
  • 2
  • 29
  • 42
Pizzaboy
  • 331
  • 2
  • 14
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 18 '15 at 17:15
  • Sorry about that, as I said, I´m not good at all with PHP. – Pizzaboy May 18 '15 at 17:29

4 Answers4

0

The problem is:

array('item' => $arrayPlayers, 'item' => $arrayScores)

You are overwriting the item key right after you set it.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
0

I think you want to do something like this

$answer = json_encode(array('players' => $arrayPlayers, 'scores' => $arrayScores)
invisal
  • 11,075
  • 4
  • 33
  • 54
0

Ok, so I fixed it this way, I don´t know if is the best option but it works

$myObject = new stdClass();
$myObject = array(
    "players" => $arrayPlayers,
    "scores" => $arrayScores,
);

echo json_encode($myObject);
Pizzaboy
  • 331
  • 2
  • 14
  • `new stdClass();` does nothing in this case. You are just overwriting it in the next line. In other words, you aren't using an object, but json_encode doesn't require an object so that is fine. You can just use the array you created. – Devon Bessemer May 18 '15 at 17:32
0

Please stop using mysql_* instead use mysqli_* or PDO.

As per your expected out put of arrays inside object use the below code:

$answer = json_encode(array('players', 'scores'));
$answer->players = $arrayPlayers;
$answer->scores= $arrayScores;
Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33