PHP代码无法正常工作

时间:2018-03-21 04:17:54

标签: javascript php html

我想在数据库中的表格中为男性和女性的百分比创建一个饼图。 我写了下面的代码,但它没有用。请帮忙。

wp_editor

2 个答案:

答案 0 :(得分:0)

你应该在echo中添加脚本并将其放入php代码中,类似于我在下面添加了一个片段。



<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

    <?php

$mysqli = new mysqli("localhost", "root", "", "ganesh"); 
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql1 = mysqli_query($mysqli,"SELECT SUM(CASE WHEN Gender = \'Male\' THEN 1 ELSE 0 END) FROM student");
$sql2 = mysqli_query($mysqli,"SELECT SUM(CASE WHEN Gender = \'Female\' THEN 1 ELSE 0 END) as Female_count FROM student");
echo "
<script type='text/javascript'>

    google.charts.load('current', {'packages':['corechart']});
    google.charts.setOnLoadCallback(drawChart);      
    function drawChart() {
    var data = google.visualization.arrayToDataTable([
    ['Gender', 'Number'],
    ['Male',   ".$sql1."],
    ['Female',  ".$sql2."]
    ]);
    var options = {
    title: 'First Year'
    };
    var chart = new google.visualization.PieChart(document.getElementById('piechart'));
    chart.draw(data, options);
    }
</script>
";

?>

  <div id="piechart" style="width: 900px; height: 500px;"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

获取新的更新代码:

  <?php

$mysqli = new mysqli("localhost", "root", "", "ganesh"); 
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$sql1 = mysqli_query($mysqli,"SELECT SUM(CASE WHEN Gender = \'Male\' THEN 1 ELSE 0 END) FROM student");
$sql2 = mysqli_query($mysqli,"SELECT SUM(CASE WHEN Gender = \'Female\' THEN 1 ELSE 0 END) as Female_count FROM student");

echo ' <script  type = "text/javascript"  src="https://www.gstatic.com/charts/loader.js"></script>';

echo "
<script type='text/javascript'>

google.charts.setOnLoadCallback(drawChart);      
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Gender', 'Number'],
['Male',   ".$sql1."],
['Female',  ".$sql2."]
]);
var options = {
title: 'First Year'
 };
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
";

?>