Solved

Creation of Google Line Chart with data from MySQL

Posted on 2012-04-03
1
1,497 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 108

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now