I have a MYSQL query that returns data using PDO::FETCHASSOC. The array is then encoded using json.encode.
$row = $s->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($row);
The result is then passed to a javascript script using:
var percents = [];
percents.push(<?php get_percent('accantonamenti irreperibili', $pdo)?>);
Using console.log I see that percents is:
[[Object { perc_worst="-100", perc_best="-33"}]]
Question: how do I get perc_worst and perc_best values assigning them to two different values? percents.perc_worst doesn't work and either percents[0].perc_worst BTW I can't get why the pair key value is created with = instead of :
Anyone can help?? Thanks!
asked Dec 29, 2014 at 13:54
user2399035user2399035
1 Answer 1
Dont do .push()
.
Instead, do:
var percents = <?php get_percent('accantonamenti irreperibili', $pdo)?>;
Try accessing the values by:
console.log(percents.perc_worst);
console.log(percents.perc_best);
Keep it simple :)
answered Dec 29, 2014 at 13:58
3 Comments
Rahul Desai
@LelioFaieta Now try
console.log(percents[0].perc_worst)
or console.log(percents[0].perc_best)
default