views:

319

answers:

1

Hi,

I'd like to create a GViz of some data in a MySql Database and I'm having problems.

Here is the source so far:

<?php 
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error 
connecting to mysql'); 
mysql_select_db($dbname); 
$int_y_pos = -1; 
$int_y_step_small = 1; 
$sql = "SELECT * from table')"; 
$sql = mysql_query($sql); 
$rownum = mysql_num_rows($sql); 
?> 
<html> 
  <head> 
    <script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/ 
script> 
    <script type="text/javascript"> 
      google.load("visualization", "1", {packages: 
["table"]}); 
      google.setOnLoadCallback(drawData); 
      function drawTable() {
        var data = new google.visualization.DataTable(); 
        data.addColumn('string', 'column1'); 
        data.addColumn('string', 'column2'); 
<?php 
echo "     data.addRows($rownum);\n"; 
while($row = mysql_fetch_assoc($sql)) { 
        $int_y_pos += $int_y_step_small; 
        echo "     data.setValue(" . $int_y_pos . ", 0, new column1(" . 
$row['column1']  . "));\n"; 
        echo "     data.setValue(" . $int_y_pos . ", 0, new column2(" . 
$row['column2']  . "));\n";  
} 

?> 
  var table = new google.visualization.Table(document.getElementById('table_div'));
  table.draw(data, {showRowNumber: true});

  google.visualization.events.addListener(table, 'select', function() {
    var row = table.getSelection()[0].row;
    alert('You selected ' + data.getValue(row, 0));
  });
} 
 </script> 
  </head> 
  <body> 
    <div id="table_div" style="width: 940px; height: 240px;"></div> 
  </body> 
</html>

Any help or examples of previous previous integrations would be great.

Thanks

Gareth

A: 

Thank you for your great script, whoever it did not work for me ...

I changed it to look like this.

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$int_y_pos = -1; 
$int_y_step_small = 1;
$sql = "SELECT * from cache"; 
$sql = mysql_query($sql);
$rownum = mysql_num_rows($sql);
?> 
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Google Visualization API Sample
    </title>
    <script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['table']});
    </script>
    <script type="text/javascript">
    function drawVisualization() {
      // Create and populate the data table.
      var data = new google.visualization.DataTable();
        data.addColumn('string', 'col1');
        data.addColumn('string', 'col2');
<?php
echo "     data.addRows($rownum);\n";
while($row = mysql_fetch_assoc($sql)) {
        $int_y_pos += $int_y_step_small; 
        echo "     data.setCell(" . $int_y_pos . ", 0,'". $row['col1']  . "');\n"; 
        echo "     data.setCell(" . $int_y_pos . ", 1,'" . $row['col2']  . "');\n"; 
} 

?> 
 // Create and draw the visualization.
      visualization = new google.visualization.Table(document.getElementById('table'));
      visualization.draw(data, null);
    }


    google.setOnLoadCallback(drawVisualization);
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="table"></div>                  
  </body>
</html>
kithros