Click here to Skip to main content
15,948,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
$query = "SELECT name FROM mybase WHERE name = '$name' ";

        $mysqli = new mysqli();
        $mysqli->connect('localhost', 'blabla', 'blabla', 'blabla');
        $result = $mysqli->query($query);
        while ($row = $result->fetch_assoc()) {
            //here I cant figure out how to make data in format for pie chart
        }


<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 = google.visualization.arrayToDataTable([
          ['Name', 'Seen x times'],
          ['somenamefrommysql',     11],
          ['somenamefrommysql',      2],
          ['somenamefrommysql',  2],
          ['somenamefrommysql', 2],
          ['somenamefrommysql',    7]
        ]);

        var options = {
          title: 'My Daily Activities'
        };

        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>
Posted
Updated 1-May-14 9:02am
v2

1 solution

I try to write you some pseudocode.. Try to do something like this.
For the first, create new class Result for example.. There will be your rows from database in one array..


Class Result
{
  private $rowName;
  private $rowValue;

  _construct Result()
  {
    $this->rowName="";
    $this->rowValue="";
  }

  /*
  getters and setters will be here
  */
}


$query = "SELECT name FROM mybase WHERE name = '$name' ";
 

        $mysqli = new mysqli();
        $mysqli->connect('localhost', 'blabla', 'blabla', 'blabla');
        $result = $mysqli->query($query);
        
        $arrayResult = array();
        while ($row = $result->fetch_assoc()) {
           $arrayResult[] = $row['rowName'];  //rows will be added into array
        }
 

<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 = google.visualization.arrayToDataTable([
          ['Name', 'Seen x times'],
<?php
foreach($arrayResult as $graphValue)
{
  echo "['".$graphValue.getRowName()."'], ".$grapghValue.getRowValue()." ],"...
  //here you should check if next row is the last and then -> , not will be there..
}
? >
        ]);
 
        var options = {
          title: 'My Daily Activities'
        };
 
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900