2016-12-07 4 views
0

データベースからテーブルを取得する助けを借りてGoogleの円グラフを作成したいのですが...今までは出力が得られませんでした...いくつかのウェブサイトを検索しましたが、何も...私のコードをチェックし、エラーが何を受けているされてphpmyadminから円グラフを生成できません

<?php 

mysql_connect('localhost','root',''); 
mysql_select_db('chart'); 

$sqlquery1="select * from pie_chart"; 

$sqlresult1=mysql_query($sqlquery1); 

$rows=array(); 

while($r=mysql_fetch_assoc($sqlresult1)) 
{ 
    $rows[]=$r; 
} 
$data= json_encode($rows); 
?> 
<html> 
<head> 
<!--Load the AJAX API --> 
<script type="text/javascript" src="https://www.google.com/jsapi"></script> 
<script  src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script> 

<script type="text/javascript"> 

//Load the visualization API and the piechart package 
google.load('visualization','1',{'packages':['corechart']}); 

//Set a callback to run when the google visualization API is loaded 
google.setOnLoadCallback(drawchart); 

function drawChart(){ 
var data = new google.visualization.DataTable(); 
    data.addColumn("string", "Quarter"); 
    data.addColumn("number", "Number"); 

    data.addRows(<?php echo $data ?>); 

    ]); 

//Create our data table out of JSON data loaded from server 
var data=new google.visualization.DataTable(jsonData); 

//Instantiate and draw our chart, passing in some options 
var chart=new  google.visualization.PieChart(document.getElementById('chart_div')); 
    chart.draw(data,{width:400,height:240}); 
    } 

    </script> 
    </head> 

    <body> 
    <!--Div that will hold the pie chart --> 
    <div id="chart_div"></div> 
    </body> 
    </html> 
+0

教えてください?副次的なこととして、PHP Taggingを使用してJavascriptにデータを追加する場合は、ECHOを使用する必要があります。したがって、あなたの "data.addRows(<?php $ data?>);"これを "data.addRows(<?php echo $ data?>);"に変更してください。 – Strahdvonzar

+0

私はそれを変更しますが、警告が表示されます:mysql_fetch_assoc()はパラメータ1がリソースであることを期待しています.12行目のC:\ xampp \ htdocs \ programs \ chart2.phpで指定されたブール。 – manikandan

+0

mysqlドライバPHPで削除され、PhP 7.0で使用できなくなるため、 "http://php.net/manual/en/mysqli-result.fetch-assoc.php"をチェックし、それに従ってコードを変更してください。失敗した場合、mysql_queryはboolean(false)を返します。おそらくクエリが失敗し、結果を返さないため、警告が表示されます。 – Strahdvonzar

答えて

0
<?php 
/* Establish the database connection */ 
$mysql =mysqli_connect('localhost', 'root', '', 'chart'); 
/* select all the tasks from the table piechart */ 
$result = $mysql->query('SELECT * FROM piechart'); 

/* 
    --------------------------- 
    example data: Table (piechart) 
    -------------------------- 
    Task  percent 
    job    30 
    daily   20 
    working   35 
    sleep   15  
*/ 


$rows = array(); 
$table = array(); 
$table['cols'] = array(

// Labels for your chart, these represent the column titles. 
/* 
    note that one column is in "string" format and another one is in "number" format 
    as pie chart only required "numbers" for calculating percentage 
    and string will be used for Slice title 
*/ 
       array('label' => 'task', 'type' => 'string'), 
       array('label' => 'percent', 'type' => 'number') 
       ); 

/* Extract the information from $result */ 
/* associate array are used in above ,so we used foreach loop*/ 
foreach($result as $r) 
{ 
    $temp = array(); 

    // The following line will be used to slice the Pie chart 

    $temp[] = array('v' => (string) $r['task']); 

    // Values of the each slice 

    $temp[] = array('v' => (int) $r['percent']); 

    //rows of side title 
    $rows[] = array('c' => $temp); 
} 
    $table['rows'] = $rows; 

    // convert data into JSON format 
    $jsonTable = json_encode($table); 
    //echo $jsonTable; 

?> 

<html> 
<head> 
<!--Load the Ajax API--> 
<script type="text/javascript" src="https://www.google.com/jsapi"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
<script type="text/javascript"> 

// Load the Visualization API and the piechart package. 
google.load('visualization', '1', {'packages':['corechart']}); 

// Set a callback to run when the Google Visualization API is loaded. 
google.setOnLoadCallback(drawChart); 

// Load the Visualization API and the piechart package. 
function drawChart() 
{ 
// Create our data table out of JSON data loaded from server. 
var bar_chart_data = new google.visualization.DataTable(<?php echo $jsonTable; ?>); 

var options = { 
       title: 'Daily Work log', 
       is3D: 'true',//3D effect true 
       width: 900, 
       height: 500 
       }; 

    // Instantiate and draw our chart, passing in some options. 
    // Do not forget to check your div ID 
var chart = new google.visualization.PieChart(document.getElementById('chart_div')); 

chart.draw(bar_chart_data, options); 
} 

    </script> 
</head> 
      <body> 
      <!--this is the div that will hold the pie chart--> 
       <div id="chart_div" ></div> 
      </body> 
     </html> 
    </script> 
</head> 

関連する問題