设置Highcharts头标时,无法在JS脚本中获取PHP变量 [英] Can't get PHP variables in the JS script when setting Highcharts head tag

查看:308
本文介绍了设置Highcharts头标时,无法在JS脚本中获取PHP变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的php文件( recherche.php )的一个摘录,其中包含 Html Javascript JQuery Highcharts )代码并运行 MySQL 查询。



但是,当在头部中设置JQuery代码时,我无法访问 PHP / code>标记。

 < html> 
< head>
< meta http-equiv =content-typecontent =text / html; charset = UTF-8/>
< title>< / title>

< link href =../ css / bootstrap.csstype =text / css =stylesheet/>
< link href =../ css / style.csstype =text / css =stylesheet/>

< script src =./ graphics / js / jquery-1.9.1.jstype =text / javascript>< / script>
< script src =./ graphics / js / highcharts.jstype =text / javascript>< / script>

< script type =text / javascript>

var cat ='<?php echo $ categorys; →';
var data1 ='<?php echo $ datas; →';
var data2 ='<?php echo $ datas2; →';
var data3 ='<?php echo $ datas3; →';

var chart;
$(document).ready(function(){

chart1 = new Highcharts.Chart({

图表:{
renderTo:'containerxx ',
类型:'bar'
},

标题:{
text:'行业的平均工资'
},

xAxis:{
类别:['女','男']
},

y轴线:{
title:{
文本:'工资'
}
},

系列:[{
name:'average salary',
data:document.write( data1);
},
{
名称:'最高工资',
data:document.write(data2);
},
{
名称:'min salary',
data:document.write(data3);
}]

});

});

< / script>





...以下,仍然在同一个文件中,并且在 body 标记中,在更多代码之后,是php变量分配: <$ p
$ b

  ... 
while($ row = mysqli_fetch_array($ result,MYSQLI_ASSOC)){

$ category [] = $ row [$ categorie];

$ data [] =(INT)$ row ['Salaire_moyen'];

$ data2 [] =(INT)$ row ['Salaire_Max'];

$ data3 [] =(INT)$ row ['Salaire_Min'];
}
$ categorys = json_encode($ category);
echo $ categorys;
echo< br />< br />;
$ datas = json_encode($ data);
echo $ datas;
echo< br />< br />;
$ datas2 = json_encode($ data2);
echo $ datas2;
echo< br />< br />;
$ datas3 = json_encode($ data3);
echo $ datas3;
echo< br />< br />;
....

这是启动highchart图形的命令: p>

 < div id =containerxxstyle =width:100%; height:400px; margin:0 auto><< ; / DIV> 

但遗憾的是,屏幕上没有显示任何图表。

但是当我在同一个php文件中的静态值做同样的工作时,我可以在我的网页中获取高图形。



例如,当我在同一个文件中编写下面的脚本而不是上面的脚本时,它会起作用,图形将显示在我的页面上:

 < script type =text / javascript> 
var chart;
$(document).ready(function(){


chart1 = new Highcharts.Chart({

图:{
renderTo:'containerxx',

类型:'bar'
},

标题:{
text:'Salaire moyen desmétiersde l\\ \\'informatique'
},

xAxis:{
categories:['Architecte logiciel','Développeurlogiciel','Infogérance']
},

yAxis:{
title:{
text:'Salaire moyen'
}
},


系列:[{
name:'Femmes',
data:[60000,45000,25000]
},
{
name:'Hommes',
data:[75000,48000,27500]
}]

});


});

< / script>





为了让我的图形能够动态地处理PHP变量(尽管是静态的),我该怎么办?

您必须在您的JavaScript函数中回显它们之前初始化您的变量。因此,将PHP加载到< script>


之上

Below is an extract of my php file (recherche.php) which contains Html and Javascript (JQuery Highcharts) codes and run a MySQL query.

But, I can't access the PHP variables when setting the JQuery code in the head tag.

    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title></title>

    <link href="../css/bootstrap.css" type="text/css" rel="stylesheet" />
    <link href="../css/style.css" type="text/css" rel="stylesheet" />

    <script src="./graphics/js/jquery-1.9.1.js" type="text/javascript"></script>
    <script src="./graphics/js/highcharts.js" type="text/javascript"></script>

    <script type="text/javascript">

        var cat = '<?php echo $categorys; ?>' ; 
        var data1 = '<?php echo $datas; ?>' ;
        var data2 = '<?php echo $datas2; ?>' ;
        var data3 = '<?php echo $datas3; ?>' ;

        var chart;
        $(document).ready(function() { 

            chart1 = new Highcharts.Chart({

                chart: {
                    renderTo: 'containerxx',  
                    type: 'bar'
                    },

                title: {
                    text: 'Average salaries of the sector'
                    },

                xAxis: {
                    categories:  ['Women', 'Men']
                    },

                yAxis: {
                    title: {
                        text: 'Salary'
                        }
                    },

                series: [{
                            name: 'average salary',
                            data: document.write(data1);
                        },  
                        {   
                            name: 'max salary',
                            data: document.write(data2);
                        },
                        {   
                            name: 'min salary',
                            data: document.write(data3); 
                        }]

            });

        });

    </script>

...and below, still in the same file and in the body tag, after some more code, are the php variables allocations:

    ...
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

    $category[] = $row[$categorie];

    $data[] = (INT)$row['Salaire_moyen'];

    $data2[] = (INT)$row['Salaire_Max'];

    $data3[] = (INT)$row['Salaire_Min'];
    }
$categorys = json_encode($category); 
echo $categorys ;
echo "<br/><br/>";
$datas = json_encode($data); 
echo $datas ;
echo "<br/><br/>";
$datas2 = json_encode($data2); 
echo $datas2;
echo "<br/><br/>";
$datas3 = json_encode($data3); 
echo $datas3 ;
echo "<br/><br/>";
    ....

and this is the command to launch the highchart graphics :

    <div id="containerxx" style="width:100%; height:400px; margin:0 auto"></div>

But sadly, no chart is diplayed on the screen.

But when I do the same with static values in the same php file it works and I can get the highchart graphic in my webpage.

For example, when I write this script below instead of the one above in the same file, it works and the graphic is diplayed on my page:

    <script type="text/javascript">
        var chart;
        $(document).ready(function() { 


            chart1 = new Highcharts.Chart({

                chart: {
                    renderTo: 'containerxx',  

                    type: 'bar'
                    },

                title: {
                    text: 'Salaire moyen des métiers de l\'informatique'
                    },

                xAxis: {
                    categories:  ['Architecte logiciel', 'Développeur logiciel', 'Infogérance']
                    },

                yAxis: {
                    title: {
                        text: 'Salaire moyen'
                        }
                    },


                series: [{
                            name: 'Femmes',
                            data: [60000, 45000, 25000]
                        },  
                        {   
                            name: 'Hommes',
                            data: [75000, 48000, 27500]
                        }]

            });


        });

    </script>

What do I miss to make my graphic dynamic working with PHP variables despite statics ones?

解决方案

You have to initialize your variables BEFORE echoing them in your JavaScript function. So place the PHP that loads these variables above your <script>

这篇关于设置Highcharts头标时,无法在JS脚本中获取PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆