0

Possible Duplicate:
Dynamic Pie Chart Data in Javascript and PHP

The following code is used to populate a pie chart in javascript:

<script type="text/javascript">

    var agg = { label: 'Aggressive', pct: [60, 10, 6, 30, 14, 10] },
        bal = { label: 'Balanced',   pct: [24,  7, 2, 18, 13, 36] },
        mod = { label: 'Moderate',   pct: [12,  4, 2, 10, 11, 61] },
        inc = { label: 'Income',     pct: [ 0,  0, 0,  0,  0,100] },

</script>

Instead of tm: [40, 60, 67, 30, 74, 50] as seen above, I would like to make each of 6 values a php variable. Like: tm: [$r1, $r2, $r3, $r4, $r5, $r6] . How do I do this?

How do I do this with? A php array or json encode?

1
  • Very similar questions have been asked before. Start with the Related questions to the right of this comment. Commented Oct 1, 2012 at 3:40

1 Answer 1

2

Yes, json_encode a PHP array:

var agg = <?php echo json_encode(array('label' => 'Aggressive', 'pct' => array($r1, $r2, ...))); ?>,
    bal = <?php echo json_encode(...); ?>,
    ...
1
  • Looks good, but I've tried it already. It doesnt seem to work in the code I am applying it to. Commented Oct 1, 2012 at 4:01

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.