Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creation of Google Line Chart with data from MySQL

Posted on 2012-04-03
1
Medium Priority
?
1,932 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
Suggested Courses

604 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