Solved

Creation of Google Line Chart with data from MySQL

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

895 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

14 Experts available now in Live!

Get 1:1 Help Now