Solved

Creation of Google Line Chart with data from MySQL

Posted on 2012-04-03
1
1,595 Views
Last Modified: 2012-05-12
Hi experts,

I hope someone can help me build a MySQL query. I want to extract data from the database of the number of table reservations  across all months in a specific year e.g. 2011.

All our table reservations from our nightclub is stored in a database table called reservations.

Then we have the following fields:

ID (int)
reservation_date (date)

I wonder if there is a smart way to count total reservations based on the ID and combine the date field with this graph from Google: https://google-developers.appspot.com/chart/interactive/docs/gallery/linechart

Thanks in advance!
0
Comment
Question by:sittinDuck
1 Comment
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37808050
I can't test this (don't have your data base), but maybe this will give you an idea to get started.

The general strategy is to run one query for each month, and get the month and reservation counts in an associative array.  Then we can use the array to build the data.addRows information that we need to feed to Google.  HTH, ~Ray
<?php // RAY_temp_sittinDuck.php
error_reporting(E_ALL);

// CONSTRUCT THE QUERIES
$sqls['Jan'] = "SELECT id FROM reservations WHERE reservation_date BETWEEN '2011-01-01' AND '2011-01-31';
$sqls['Feb'] = "SELECT id FROM reservations WHERE reservation_date BETWEEN '2011-02-01' AND '2011-02-28';
$sqls['Mar'] = "SELECT id FROM reservations WHERE reservation_date BETWEEN '2011-03-01' AND '2011-03-31';
$sqls['Apr'] = "SELECT id FROM reservations WHERE reservation_date BETWEEN '2011-04-01' AND '2011-04-30';
// $sqls ETC ETC

// RUN THE QUERIES
$mons = array();
foreach ($sqls as $mon => $sql)
{
    $res = mysql_query($sql) or die( "FAIL: $sql<br/>" . mysql_error() );
    $mons[$mon] = mysql_num_rows($res);
}

// SET UP THE DATA BLOCK FOR THE CHARTS
$rows = NULL;
foreach ($mons as $mon => $num)
{
    $rows .= "['" . $mon . "'," . $num . "],";
}
$rows = rtrim($rows, ',');

// CREATE THE GOOGLE CHARTS DOCUMENT
$doc = <<<HEREDOC
<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Month');
        data.addColumn('number', 'Reservations');
        data.addRows([
        $rows
        ]);

        var options = {
          title: 'Company Performance'
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>
HEREDOC;

echo $doc;

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question