3

I would like to take student's exam results and represent it as a chart for student's supervisor. So I will fetch the database information (which is student's result and represent it graphically to the supervisor to notice student's progress)

I have been looking for a way to represent that either by php coding or js but I couldn't. I found google API that drawing charts in a very nice way but it accepts only JSON tables...

Thank you

Aisha
  • 93
  • 1
  • 6

4 Answers4

1

Just use the export function in phpmyadmin (here you can find which formats are supported). Select the EXPORT button in the top of the page (of the table you'd like to export) and then change the format from SQL to JSON.

If you want to export data automatically (don't tag phpmyadmin), you should just excract them from the database and then encode them in json format (json_encode()). Then just take them to Google API and retrieve the charts.

Kei
  • 771
  • 6
  • 17
  • You mean every time i would like to represent data i should export it! I would like the charts be represented dynamically, I will try this, thank you – Aisha Dec 08 '13 at 14:18
1

If you are working with a PHP backend, you can query the database to get a result set and put that result set into a format the Google Visualization API can understand, then draw charts based on it. There is no need to export the data every time you need to draw a chart:

<?php
$username = 'mysqlusername';
$password = 'password';
$databasename = 'school';
try {
    $db = new PDO("mysql:dbname=$databasename", $username, $password);
}
catch (PDOException $e) {
    die ($e->getMessage());
}
$query = $db-> prepare('SELECT student, grade FROM myClass WHERE year = :year AND semester = :semester');
// 'year' and 'semester' here get substituted into the query for ':year' and ':semester' respectively
// this is a secure way of passing in parameters to the query so that a malicious user cannot use SQL injection to penetrate your security
$query->execute(array('year' => 2013, 'semester' => 1));
$results = $query->fetchAll(PDO::FETCH_ASSOC);

$data = array(
    // create whatever columns are necessary for your charts here
    'cols' => array(
        array('type' => 'string', 'label' => 'Student Name'),
        array('type' => 'number', 'label' => 'Grade')
    )
    'rows' => array()
);

foreach ($results as $row) {
    // 'student' and 'grade' here refer to the column names in the SQL query
    $data['rows'][] = array('c' => array(
        array('v' => $row['student']),
        array('v' => $row['grade'])
    });
}
?>
<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
            function drawChart() {
                var data = new google.visualization.DataTable(<?php echo json_encode($data, JSON_NUMERIC_CHECK); ?>);
                var chart = new google.visualization.ColumnChart(document.querySelector('#chart_div'));
                chart.draw(data, {
                    height: 400,
                    width: 600
                });
            }
            google.load('visualization', '1', {packages:['corechart'], callback: drawChart});
        </script>
    </head>
    <body>
        <div id="chart_div"></div>
    </body>
</html>
asgallant
  • 26,060
  • 6
  • 72
  • 87
0

phpMyAdmin (version 3.4.0) can generate simple charts, see https://docs.phpmyadmin.net/en/latest/charts.html.

Erasmus Cedernaes
  • 1,787
  • 19
  • 15
Marc Delisle
  • 8,879
  • 3
  • 29
  • 29
0

If the phpMyAdmin charting features are not advanced enough, and you don't need a web-interface, you could also use Excel PowerPivot to connect to your MySQL (or any other) database.

There is a very good HOWTO on setting up the link here:
http://www.joyofdata.de/blog/how-to-set-up-powerpivot-and-make-it-talk-to-mysql/

Wouter
  • 1,829
  • 3
  • 28
  • 34