莫里斯图未显示页面为空白

这是我从 mysql 获取数据然后将数据值导入到 morrisjs 的简单代码,但我的页面是纯空白的,什么也没有显示。我是新来的


<?php

        

    $conn=mysqli_connect("localhost","root","","userchart");

    $query= "SELECT * FROM chart";

    $result=mysqli_query($conn,$query);

    $chart_data='';

    while($row=mysqli_fetch_array($result))

    {

        $time= strtotime($row['time']);

        

        $chart_data .="{ user:'".$row['uid']."', time:".date('i',$time)."}";

    }

       


    echo $chart_data = substr($chart_data, 0, -1);

?>

    

我使用此语句来测试我是否以正确的格式获取值并且格式正确,但最后未显示折线图。


echo $chart_data = substr($chart_data, 0, -1);


<html>

    <head>

        <title> CHART USING MORRIS.JS</title>

        <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

        <script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>

        <script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>

    </head>

    <body>


        <div id="chart" style ="height 250px;"></div>


        <script>

            new Morris.Line({

                // ID of the element in which to draw the chart.

                element: 'chart',

                // Chart data records -- each entry in this array corresponds to a point on

                // the chart.

                data: [<?php echo $chart_data; ?>], 


                // The name of the data record attribute that contains x-values.

                xkey: 'user',

                // A list of names of data record attributes that contain y-values.

                ykeys: ['time'],

                // Labels for the ykeys -- will be displayed when you hover over the

                // chart.

                labels: ['time']

            });

        </script>

    </body>

</html>


跃然一笑
浏览 127回答 1
1回答

慕虎7371278

未经测试,但也许你可以尝试这样。您应该使用而不是进行一些片状字符串操作json_encode<?php&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $conn=mysqli_connect("localhost","root","","userchart");&nbsp; &nbsp; $query= "SELECT * FROM chart";&nbsp; &nbsp; $result=mysqli_query($conn,$query);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $data=array();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; while( $row=mysqli_fetch_array($result) ){&nbsp; &nbsp; &nbsp; &nbsp; $data[]=array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user'&nbsp; =>&nbsp; $row['uid'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'time'&nbsp; =>&nbsp; date('i',strtotime( $row['time']) )&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp; &nbsp; $json=json_encode( $data );?><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <title>CHART USING MORRIS.JS</title>&nbsp; &nbsp; &nbsp; &nbsp; <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">&nbsp; &nbsp; &nbsp; &nbsp; <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>&nbsp; &nbsp; &nbsp; &nbsp; <script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>&nbsp; &nbsp; &nbsp; &nbsp; <script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <div id="chart" style ="height 250px;"></div>&nbsp; &nbsp; &nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf('var json=%s;',$json);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Morris.Line({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element: 'chart',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:json,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xkey:'user',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ykeys:['time'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labels:['time']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP