我想连接这两个php代码,如果我们从下拉菜单中选择任何选项,那么应该为特定行显示与该选项相关的饼图。
使用php和postgresql的饼图代码。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pie Chart Demo (LibChart)- https://codeofaninja.com/</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" />
</head>
<body>
<br></br>
<?php
包括libchart.php
include "C:/xampp/htdocs/tools/libchart/libchart/classes/libchart.php";
$chart = new PieChart( 500, 300 );
$dataSet = new XYDataSet();
与postgresql建立连接
$host = 'localhost';
$port = '5432';
$database = 'sustainable';
$user = 'postgres';
$password = 'postgres';
$connectString = 'host=' . $host . ' port=' . $port . ' dbname=' . $database .
' user=' . $user . ' password=' . $password;
$link = pg_connect ($connectString);
if (!$link)
{
die('Error: Could not connect: ' . pg_last_error());
}
$query = 'select * from sustainable_development';
$result = pg_query($query);
$i = 0;
if($i < pg_num_fields($result))
{
while( $row = pg_fetch_assoc($result) ){
extract($row);
$dataSet->addPoint(new Point("{$sdg_4}", $sdg_4));
$dataSet->addPoint(new Point("{$sdg_5}",$sdg_5));
break;
}
$chart->setDataSet($dataSet);
$chart->setTitle("SDG score for SDG_4 and SDG_5");
$chart->render("1.png");
echo "<img alt='Pie chart' src='1.png' style='border: 1px solid gray;'/>";
}else{
echo "No programming languages found in the database.";
}
pg_free_result($result);
?>
</body>
</html>
使用php和postgresql的下拉菜单代码
?php
$host = 'localhost';
$port = '5432';
$database = 'sustainable';
$user = 'postgres';
$password = 'postgres';
$connectString = 'host=' . $host . ' port=' . $port . ' dbname=' . $database .
' user=' . $user . ' password=' . $password;
$link = pg_connect ($connectString);
if (!$link)
{
die('Error: Could not connect: ' . pg_last_error());
}
$query='select id, location from sustainable_development';
$result = pg_query($query);
echo "<html>";
echo "<body>";
echo "<select name='id'>";
while ($row = pg_fetch_assoc($result)) {
unset($id, $name);
$id = $row['id'];
$name = $row['location'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
?>