Solved

Creation of Google Line Chart with data from MySQL

Posted on 2012-04-03
1
1,769 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 110

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

719 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