使用php和sql时,Google Charts不会显示 [英] Google Charts won't display when using php and sql

查看:112
本文介绍了使用php和sql时,Google Charts不会显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google图表创建了一个简单的图表。我在执行数据时使用PHP和MYSQL。我正在寻找答案并尝试过,但没有显示图表



这是我的 test.php 文件

 < html> 
< head>
< script type =text / javascriptsrc =https://www.gstatic.com/charts/loader.js>< / script>
< script type =text / javascript>
google.charts.load('current',{packages:['corechart']});
google.charts.setOnLoadCallback(drawChart);

函数drawChart(){
//定义要绘制的图表。
var data = google.visualization.arrayToDataTable([
['Specialization','Facultyno'],
<?php
require_once('../ mydb_connect.php' );
$ query =SELECT distinct(s.specializationname)as'specialization',count(fs.facultyid)as'facultyno'from thesisdb.specialization s join facultyspecialization fs on s.specializationid = fs.specializationid group by s.specializationID;;

$ result = mysqli_query($ dbc,$ query);
while($ row = mysqli_fetch_array($ result)){

echo['。$ row ['specialization']。',。$ row ['facultyno']。],;
}
?>

]);
var options = {'title':'教师专业化数量',
'width':1300,
'height':900};

}
//实例化并绘制图表。
var chart = new google.visualization.ColumnChart(document.getElementById('no_of_specialization'));
chart.draw(data,options);
}
< / script>
< / head>

< body>
< div id =no_of_specialization/>
< / body>
< / html>

寻找问题时,我正在测试数据是否可以执行并且工作



这里是PHP代码

 <?php 
require_once ( '../mydb_connect.php');
$ query =SELECT distinct(s.specializationname)as specialization,count(fs.facultyid)as facultyno from thesisdb.specialization s join facultyspecialization fs on s.specializationid = fs.specializationid group by s.specializationID;;

$ result = mysqli_query($ dbc,$ query);
while($ row = mysqli_fetch_array($ result))
{
echo{$ row ['specialization']};
echo - ;
echo{$ row ['facultyno']}< br>;
}
?>

这是应在Google图表上显示的数据

  • 人工智能 - 1

  • Biocomputation - 3

  • 计算机和网络安全 - 2

  • 交互 - 3

  • 信息管理和分析 - 1

  • 移动和互联网计算 - 1
  • World Computing - 1
  • 软件理论 - 2

  • 理论计算机科学 - 1


  • 我需要你的帮助。谢谢

    解决方案

    代码中的每件事情都可以。但你正在使用一个额外的治疗大括号}。在这段代码后删除额外的括号'}'。

      var options = {'title':'Faculty Specializations',
    'width':1300,
    'height':900};

    如果您遇到问题以寻找额外的大括号}。只需复制并粘贴以下代码即可。

     < html> 
    < head>
    < script type =text / javascriptsrc =https://www.gstatic.com/charts/loader.js>< / script>
    < script type =text / javascript>
    google.charts.load('current',{packages:['corechart']});
    google.charts.setOnLoadCallback(drawChart);

    函数drawChart(){
    //定义要绘制的图表。
    var data = google.visualization.arrayToDataTable([
    ['Specialization','Facultyno'],
    <?php
    require_once('../ mydb_connect.php' );
    $ query =SELECT distinct(s.specializationname)as'specialization',count(fs.facultyid)as'facultyno'from thesisdb.specialization s join facultyspecialization fs on s.specializationid = fs.specializationid group by s.specializationID;;

    $ result = mysqli_query($ dbc,$ query);
    while($ row = mysqli_fetch_array($ result)){

    echo['。$ row ['specialization']。',。$ row ['facultyno']。],;
    }
    ?>

    ]);
    var options = {'title':'教师专业化数量',
    'width':1300,
    'height':900};

    //实例化并绘制图表。
    var chart = new google.visualization.ColumnChart(document.getElementById('no_of_specialization'));
    chart.draw(data,options);
    }
    < / script>
    < / head>

    < body>
    < div id =no_of_specialization/>
    < / body>
    < / html>


    I'm creating a simple chart using Google Charts. I am using PHP and MYSQL when executing data. I was searching for answers and tried but it does not display the chart

    Here is my test.php file

    <html>
        <head>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript">
          google.charts.load('current', {packages: ['corechart']});
          google.charts.setOnLoadCallback(drawChart);
    
          function drawChart() {
          // Define the chart to be drawn.
          var data = google.visualization.arrayToDataTable([
            ['Specialization', 'Facultyno'],
            <?php
            require_once('../mydb_connect.php');
            $query="SELECT distinct(s.specializationname) as 'specialization', count(fs.facultyid) as 'facultyno' from thesisdb.specialization s join facultyspecialization fs on s.specializationid=fs.specializationid group by s.specializationID;";
    
            $result = mysqli_query($dbc,$query);
            while($row = mysqli_fetch_array($result)){
    
            echo "['".$row['specialization']."',".$row['facultyno']."],";
            } 
            ?>
    
            ]);
            var options = {'title':'Number of Faculty Specializations',
                           'width':1300,
                           'height':900};
    
          }
          // Instantiate and draw the chart.
          var chart = new google.visualization.ColumnChart(document.getElementById('no_of_specialization'));
          chart.draw(data, options);
        }
        </script>
        </head>
    
        <body>
        <div id="no_of_specialization"/>
        </body>
    </html>
    

    When looking for the problem, I was testing if the data can be executed and It worked

    here is the php code

    <?php
            require_once('../mydb_connect.php');
            $query="SELECT distinct(s.specializationname) as specialization, count(fs.facultyid) as facultyno from thesisdb.specialization s join facultyspecialization fs on s.specializationid=fs.specializationid group by s.specializationID;";
    
            $result = mysqli_query($dbc,$query);
            while($row = mysqli_fetch_array($result))  
                              {  
                                   echo "{$row['specialization']}";
                                   echo " - ";
                                   echo "{$row['facultyno']}<br>";  
                              } 
            ?>
    

    This is the data that should be displayed on Google Charts

  • Artificial Intelligence - 1
  • Biocomputation - 3
  • Computer and Network Security - 2
  • Human-Computer Interaction - 3
  • Information Management and Analytics - 1
  • Mobile and Internet Computing - 1
  • Real-World Computing - 1
  • Software Theory - 2
  • Theoretical Computer Science - 1
  • I need your help. Thanks

    解决方案

    Every thing is ok in your code. but you are using one extra curely braces "}". remove the extra curely braces '}' after this code.

    var options = {'title':'Number of Faculty Specializations',
                           'width':1300,
                           'height':900};
    

    if you faced problem to finding the extra curely braces "}". just copy and paste below code.

    <html>
    <head>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript">
            google.charts.load('current', {packages: ['corechart']});
            google.charts.setOnLoadCallback(drawChart);
    
            function drawChart() {
                // Define the chart to be drawn.
                var data = google.visualization.arrayToDataTable([
                    ['Specialization', 'Facultyno'],
                    <?php
                    require_once('../mydb_connect.php');
                    $query="SELECT distinct(s.specializationname) as 'specialization', count(fs.facultyid) as 'facultyno' from thesisdb.specialization s join facultyspecialization fs on s.specializationid=fs.specializationid group by s.specializationID;";
    
                    $result = mysqli_query($dbc,$query);
                    while($row = mysqli_fetch_array($result)){
    
                        echo "['".$row['specialization']."',".$row['facultyno']."],";
                    }
                    ?>
    
                ]);
                var options = {'title':'Number of Faculty Specializations',
                    'width':1300,
                    'height':900};
    
            // Instantiate and draw the chart.
            var chart = new google.visualization.ColumnChart(document.getElementById('no_of_specialization'));
            chart.draw(data, options);
            }
        </script>
    </head>
    
    <body>
    <div id="no_of_specialization"/>
    </body>
    </html>
    

    这篇关于使用php和sql时,Google Charts不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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