将数据从localhost(phpmyadmin)导入到网页“Highcharts”(作为phpmyadmin中的项目)

时间:2015-07-30 11:36:47

标签: mysql highcharts phpmyadmin

这是使用来自Phpmyadmin的数据在网页中显示列范围(高图中的图形)的代码。连接是完整的,但是当我们向DB请求时,会显示任何内容。你可以帮我吗? 代码:

<!DOCTYPE HTML>
<?php

                  $db = mysqli_connect('localhost', 'root') or die("Error " . mysqli_error($db)); 
mysqli_set_charset($db,"utf8");

// on sélectionne la base 
$db_selected=mysqli_select_db($db,'outil_échantillonnage_et_analyse_de_données') or die("Error in the consult.." . mysqli_error($db)); 
?>
<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>
        <style type="text/css">
${demo.css}
        </style>
        <script type="text/javascript">
$(function () {

    $('#container').highcharts({

        chart: {
            type: 'columnrange',
            inverted: true
        },

        title: {
            text: 'Moyenne age'
        },

        subtitle: {
            text: 'informations sur la moyenne d age des participants'
        },

        xAxis: {
            categories: []
        },

        yAxis: {
            title: {
                text: 'Age ( ans )'
            }
        },

        tooltip: {
            valueSuffix: 'ans'
        },

        plotOptions: {
            columnrange: {
                dataLabels: {
                    enabled: true,
                    formatter: function () {
                        return this.y + 'ans';
                    }
                }
            }
        },

        legend: {
            enabled: false
        },

        series: [{
            name: 'moyenne age',
            data: [<?php

// on crée la requête SQL 
$sql = 'SELECT question2 FROM questionnaire'; 
 do {  echo $data['question2'];  } while ($data = mysql_fetch_assoc($sql)); 

// on envoie la requête 
$req = mysqli_query($db, $sql) ; 

                                 do { ?><?php switch ($data['question2']) {
                                case 'moins de 18ans':?>
                                    [0.0,18.0],
                                   <?php break;
                                case '18-35 ans':?>
                                    [18.0,35.0],
                                    <?php break;
                                case '35-60 ans':?>
                                     [35.0,60.0];
                                   <?php break;
                                case 'plus de 60ans':?>
                                    [60.0,100.0];
                                   <?php break;
                            }; ?>,<?php } while ($data = mysqli_fetch_assoc($req); 

                     ?>,

            ]
        }]

    });

});
<?php 
mysqli_close($db); 
?>

        </script>
    </head>
    <body>
<script src="../../js/highcharts.js"></script>
<script src="../../js/highcharts-more.js"></script>
<script src="../../js/modules/exporting.js"></script>

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


    </body>
</html>}

1 个答案:

答案 0 :(得分:0)

我认为问题在于:
// on crée la requête SQL $sql = 'SELECT question2 FROM questionnaire'; do { echo $data['question2']; } while ($data = mysql_fetch_assoc($sql)); 你在这里打印一些东西。
如果我们看一下highcharts的演示,它看起来像:
series: [{ name: 'Tokyo', data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6] }
但是在您的代码上看起来就像是在打印[35.0,60.0];之前打印了一些东西 还有一个错误(我认为)你有data: [<?php而你再次打印[35.0,60.0];我认为你的数据看起来像[[35.0, 60.0]]
如果你在控制台中查看(在chrome上按ctrl + shift + j),那里不会出现错误吗?

CODE

    <!DOCTYPE HTML>
    <?php

                      $db = mysqli_connect('localhost', 'root') or die("Error " . mysqli_error($db)); 
    mysqli_set_charset($db,"utf8");

    // on sélectionne la base 
    $db_selected=mysqli_select_db($db,'outil_échantillonnage_et_analyse_de_données') or die("Error in the consult.." . mysqli_error($db)); 
    ?>
    <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>
            <style type="text/css">
    ${demo.css}
            </style>
            <script type="text/javascript">
    $(function () {

        $('#container').highcharts({

            chart: {
                type: 'columnrange',
                inverted: true
            },

            title: {
                text: 'Moyenne age'
            },

            subtitle: {
                text: 'informations sur la moyenne d age des participants'
            },

            xAxis: {
                categories: []
            },

            yAxis: {
                title: {
                    text: 'Age ( ans )'
                }
            },

            tooltip: {
                valueSuffix: 'ans'
            },

            plotOptions: {
                columnrange: {
                    dataLabels: {
                        enabled: true,
                        formatter: function () {
                            return this.y + 'ans';
                        }
                    }
                }
            },

            legend: {
                enabled: false
            },

            series: [{
                name: 'moyenne age',
                data: [<?php

    // on crée la requête SQL 
    $sql = 'SELECT question2 FROM questionnaire'; 

    // on envoie la requête 
    $req = mysqli_query($db, $sql) ; 

                                     do { ?><?php switch ($data['question2']) {
                                    case 'moins de 18ans':?>
                                        [0.0,18.0],
                                       <?php break;
                                    case '18-35 ans':?>
                                        [18.0,35.0],
                                        <?php break;
                                    case '35-60 ans':?>
                                         [35.0,60.0];
                                       <?php break;
                                    case 'plus de 60ans':?>
                                        [60.0,100.0];
                                       <?php break;
                                }; ?>,<?php } while ($data = mysqli_fetch_assoc($req); 

                         ?>, 
]        
            }]

        });

    });
    <?php 
    mysqli_close($db); 
    ?>

            </script>
        </head>
        <body>
    <script src="../../js/highcharts.js"></script>
    <script src="../../js/highcharts-more.js"></script>
    <script src="../../js/modules/exporting.js"></script>

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


        </body>
    </html>}