带有Highchart的PHP和MySQL [英] Php and MySQL with Highchart

查看:45
本文介绍了带有Highchart的PHP和MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我.我是php和highcharts的新手.我尝试使用mysql和php填充图表,但是当我尝试运行图表时,该图表没有出现,我只选择了一个空白网页.而且没有出现错误.

Somebody can help me. I'm new in php and highcharts. I tried to populate my chart using mysql and php, but when I tried to run it, the chart didn't appear, I only sse a blank web page. And there's no error appeared.

她是我的验证码(对不起,代码混乱):

Her's my codes (sorry for messy code):

<!DOCTYPE HTML>
 <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Highcharts Example</title>

       <script type="text/javascript"    src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="../../js/highcharts.js"></script>
    <script src="../../js/modules/exporting.js"></script>

</head>

        <body>

    <?php
include "config.php";

$SQL1 =     "SELECT * FROM pos";

$result1 = mysql_query($SQL1);
$data1 = array();
while ($row = mysql_fetch_array($result1)) {
   $data1[] = $row['name'];
   $data2[] = $row['Qty'];

}
?>

<script type="text/javascript">
$(function () {
    $('#container').highcharts({
        chart: {
            type: 'column',
            margin: [ 50, 50, 100, 80]
        },
        title: {
            text: 'List of POS'
        },
    credits: {
    enabled: false
    },
        xAxis: {
            categories: [<?php echo join($data1, "','"); ?>],
            labels: {
                rotation: -45,
                align: 'right',
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: 'No. of Ticket'
            }
        },
        legend: {
            enabled: false,
    layout: 'vertical',
                        backgroundColor: '#FFFFFF',
                        align: 'left',
                        verticalAlign: 'top',
                        x: 50,
                        y: 35,
                        floating: true,
                        shadow: true
        },
        tooltip: {
            pointFormat: '<b>{point.y:.1f} tickets</b>',
        },
     plotOptions: {
                            column: {
                                        pointPadding: 0.2,
                                        borderWidth: 0
                                    }
                        },
        series: [{
            name: 'Qty',
            data: ['<?php echo join($data2, "','"); ?>'],
    dataLabels: {
                enabled: true,
                rotation: -90,
                color: '#FFFFFF',
                align: 'right',
                x: 4,
                y: 10,
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif',
                    textShadow: '0 0 3px black',

                }
            }
        }]
    });
});

    </script>

   <div id="container" style="min-width: 500px; height: 400px; margin: 0 auto"></div>

</body>
    </html>

这是我的config.php

And here's my config.php

 <?php
 $mysql_hostname = "localhost";
 $mysql_user = "root";
 $mysql_password = "";
 $mysql_database = "pos";
 $prefix = "";
 $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not  connect database");
 mysql_select_db($mysql_database, $bd) or die("Could not select database");

  ?>

推荐答案

空白页通常表示语法错误.您应该打开 error_reporting .

Blank pages usually mean syntax errors. You should switch error_reporting on.

错误是在构造json的echo语句中使用的.错误是您在两个echo语句中都缺少分号.

The errors are in the use of your echo statements where you construct the json. The error is that you are missing semi colons in both the echo statements.

<?php echo join($data1, ',') ?>替换为<?php echo join($data1, ','); ?>

$data2类似:

<?php echo join($data2, ','); ?>

您可以在以下代码段中进行其他改进:

Another improvement you could make in the following block:

    <?php
include "config.php";

$SQL1 =     "SELECT * FROM pos";

$result1 = mysql_query($SQL1);
$data1 = array();
while ($row = mysql_fetch_array($result1)) {
   $data1[] = $row['name'];
}

$result2 = mysql_query($SQL1);
$data2 = array();
while ($row = mysql_fetch_array($result2)) {
   $data2[] = $row['Qty'];
}
?>

代替执行两次查询来构建两个数组,您可以摆脱其中一个查询,并根据相同的查询结果来构建两个数组:

Instead of executing query twice to build two arrays, you could get rid of one of the queries and build both the arrays from the same query result:

<?php
include "config.php";

$SQL1 =     "SELECT * FROM pos";

$result1 = mysql_query($SQL1);

$data1 = array();
$data2 = array();

while ($row = mysql_fetch_array($result1)) {
   $data1[] = $row['name'];
   $data2[] = $row['Qty'];
}
?>

注意: php mysql自PHP 5.5.0起不推荐使用扩展名,您应该使用MySQLi或PDO_MySQL.

Note: The php mysql extension is deprecated as of PHP 5.5.0, you should be using either MySQLi or PDO_MySQL.

这篇关于带有Highchart的PHP和MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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