0

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
0

1 Answer 1

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

The array becomes [Object { perc_worst="-100", perc_best="-33"}] (lost the outer square braket) but the values are undefined
@LelioFaieta Now try console.log(percents[0].perc_worst) or console.log(percents[0].perc_best)
was testing it right now... it works. you are right, better keep it simple! :D

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.