0

I have a php file and I connect it to the MySQL and fetch some values. I want to use these values in a .tpl file for showing graphs for which I am using Highcharts. I somehow am not being able to pass php values to the graph which is javascript. Please have a look at the code and tell me where am I going wrong.

My php code is:

$sqlStr = "select * from users where user_id=".$_SESSION['user_id'];

        $sqlQuery = mysql_query($sqlStr) or die(mysql_error()."<hr>".$sqlStr);

        if ( mysql_num_rows($sqlQuery) ) {

            $rowMyReport = mysql_fetch_assoc($sqlQuery);

            $smarty->assign("value1",$rowMyReport['value1']);
            $smarty->assign("value2",$rowMyReport['value2']);
            $smarty->assign("value3",$rowMyReport['value3']);
            $smarty->assign("value4",$rowMyReport['value4']);
            }  

Now I want to use these values for Highcharts to have a graph on my website. So I tried this:

$(function () {
$('#graph').highcharts({
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: 1,//null,
        plotShadow: false
    },
    title: {
        text: 'Browser market shares at a specific website, 2014'
    },
    tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                style: {
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                }
            }
        }
    },
    series: [{
        type: 'pie',
        name: 'Some-name',
        data: [
            ['title1',  <?php echo $value1; ?>],
            ['title2',       <?php echo $value2; ?>],
            ['title3',   <?php echo $value3; ?>],
            ['title4',    <?php echo $value4; ?>]
        ]
    }]
});
});

I call this graph in the .tpl file as such:

<div id="graph" style="min-width: 200px; height: 200px; margin: 0 auto"></div>

By giving hard-coded values the graph gets displayed, but when I use dynamic values(php values) graph does not displayed. Any help would be appreciated...

1
  • Probably the problem is that your values are string, instead of numbers. So use json_encode(). For more information visit our website Commented Nov 26, 2014 at 9:17

1 Answer 1

2

use json_encode for data , in your controller before passing it.

Sign up to request clarification or add additional context in comments.

4 Comments

Could you please help me with the json_encode by giving an example from my problem. As in how do I access the individual values if I need them...
$smarty->assign("value1",json_encode($rowMyReport['value1'])); when you use highchart , you must json_encode(your_data) and pass it.
So I changed it in php and in .tpl, I used <?php echo json_encode($value1)?> .. Still the chart is not visible.
in your tpl file use <?php echo $value1;?>

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.