0
jquery code

$("#pendingChart").sparkline([9, 11, 12, 13, 12, 13, 10], {
                type: 'bar',
                width: '100',
                barWidth: 6,
                height: '45',
                barColor: '#5C9BD1',
                negBarColor: '#e02222'
            });

This works fine and produce a graphical chart. But I want to produce the data [9, 11, 12, 13, 12, 13, 10] from controller and pass to the jquery code. My controller section is okay. Here is the code:

function loadPointChart() {
        $reseller = $this->Auth->user();
        if (count($reseller) > 0) {
            $api_key = $reseller['api_key'];
            $this->loadModel('Order');
            //  $info=$this->Order->find('all','conditions' => array('api_key'=>$api_key));

            $sql = "SELECT orders.id,orders.modified,orders.status,order_products.*  FROM orders
                LEFT JOIN order_products ON orders.id=order_products.order_id
                 WHERE orders.api_key = '$api_key' ORDER BY orders.modified ASC";

            $infos = $this->Order->query($sql);

            $return['points'] = $this->pointsCalculate($infos);

            $penaltyChart = json_encode($return['points']['penalty']);
            $pendingChart = json_encode($return['points']['pending']);
            $successChart = json_encode($return['points']['success']);
             echo $penaltyChart;
            $this->set(compact('penaltyChart'));
        }
    }

echo echo $penaltyChart; is printing exact format data which I need: [4,3]

But I would I manipulate this data into my jquery code. I called this function inside before filter so that variable is initialized before page rendered.

to load data into jquery section I tried as follows:

$("#penaltyChart").sparkline(<?php echo $penaltyChart; ?>, {
                type: 'bar',
                width: '100',
                barWidth: 6,
                height: '45',
                barColor: '#F36A5B',
                negBarColor: '#e02222'
            });

It gives error: Uncaught SyntaxError: Unexpected token < . Any idea?

8
  • It does not work is not a proper problem description. What does the generated source look like? What does happen? What errors are thrown? Commented Nov 11, 2015 at 20:56
  • I edited My code. Thanks to point out my fault. It will help for next time to ask Commented Nov 11, 2015 at 21:04
  • did you inspect the page source code to see what the output looks like? Commented Nov 11, 2015 at 21:06
  • I just inspect the code and the php code is kept as plain text: $("#penaltyChart").sparkline(<?php echo $penaltyChart; ?>, { Commented Nov 11, 2015 at 21:13
  • oh..there is no php compiling in a js file. You would need to pass a variable in a script tag and use that variable in your file. Or use ajax to get the data and call the plugin once the data is received Commented Nov 11, 2015 at 21:14

1 Answer 1

1

1. In load_point_chart.ctp:

<script>$("#pendingChart").sparkline([<?php echo implode(",", $penaltyChart); ?>], {
            type: 'bar',
            width: '100',
            barWidth: 6,
            height: '45',
            barColor: '#5C9BD1',
            negBarColor: '#e02222'
        });</script>

2. or in load_point_chart.ctp:

echo "<script>$('#pendingChart').sparkline([" . implode(",", $penaltyChart) . "], {
        type: 'bar',
        width: '100',
        barWidth: 6,
        height: '45',
        barColor: '#5C9BD1',
        negBarColor: '#e02222'
    });</script>";

3. or in load_point_chart.ctp before loading the .js file:

<script>window.penaltyChart = [<?php echo implode(",", $penaltyChart); ?>]</script>

after this code include your .js, and then in your .js change the code like this:

$("#pendingChart").sparkline(window.penaltyChart, {
            type: 'bar',
            width: '100',
            barWidth: 6,
            height: '45',
            barColor: '#5C9BD1',
            negBarColor: '#e02222'
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.