Morris.js Bar Graph in php?

A

Anonymous

Guest
I am using Morris.js graphs to display my MySQL data. Unfortunately the graphs are not visible at all. I have followed a simple tutorial and still nothing happens. Even the database is filled and echo works. What am I doing wrong in the code? I am assuming that the data format under Morris.Bar code should be different. What should the format be?

Code:
<?php

$connect = mysqli_connect("localhost", "host", "", "_data");
$query = "SELECT * FROM Datas";
$result = mysqli_query($connect, $query);
$chart_data = '';

while($row = mysqli_fetch_array($result))
{
    $chart_data .= "{ ID:'".$row["ID"]."', Name:".$row["Name"].", Wrongs:".$row["Wrongs"].", Rights:".$row["Rights"].", Percentage:".$row["Percentage"].", Age:".$row["Age"]."}, ";
}
$chart_data = substr($chart_data, 0);
?>

<!DOCTYPE html>
<html>
 <head>
  <title>Webslesson Tutorial | How to use Morris.js chart with PHP & Mysql</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>
  <br /><br />
  <div class="container" style="width:900px;">
   <h2 align="center">MySQL</h2>
   <h3 align="center">Data</h3>   
   <br /><br />
   <div id="chart"></div>
  </div>
 </body>
</html>


    <script>
Morris.Bar({
  element: 'chart',
  data:[<?php echo $chart_data; ?>],
  xkey: 'ID',
  ykeys: ['Wrongs', 'Rights'],
  labels: ['Wrongs', 'Rights'],
});
    </script>
 
Even the database is filled and echo works.
How have you checked this?
Code:
$chart_data = substr($chart_data, 0);
I don't understand why this is in there?
Code:
data:[<?php echo $chart_data; ?>]
Have you checked that this is in the correct format?
 
Back
Top