gpt4 book ai didi

javascript - 如何连接两个php代码

转载 作者:行者123 更新时间:2023-11-28 04:45:13 26 4
gpt4 key购买 nike

这是我从下拉菜单中选择位置并从 postgresql 数据库绘制饼图的代码。我无法在选择特定位置时绘制饼图,而是在选择位置时出现饼图,其中包含列的所有值。

创建饼图的数据库:

enter image description here

Code to select state Select.php

<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>

<script type="text/javascript">
function fetch_select(val)
{
$.ajax({
type: 'post',
url: 'connect.php',
data: {
get_option:val
},
success: function (response) {
document.getElementById("fd").innerHTML=response;
}
});
}

</script>
</body>
<div id="select">
<select onchange="fetch_select(this.value);">
<option>Select District</option>
<?php


$host = 'localhost';
$port = '5433';
$database = 'sustainable';
$user = 'postgres';
$password = 'postgis';

$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,sdg_4,sdg_5 from sustainable_development';
$result = pg_query($query);


while ($row = pg_fetch_assoc($result)) {

unset($id, $name);
$id = $row['id'];
$name = $row['location'];
echo '<option value="'.$id.'">'.$name.'</option>';

}

echo "</select>";

?>
<div id="fd">
</div>
</div>
</body>
</html>

> Code to draw pie chart
<!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
include "C:/xampp/htdocs/sdg/tools/libchart/libchart/classes/libchart.php";
$chart = new PieChart( 500, 300 );
$dataSet = new XYDataSet();
$host = 'localhost';
$port = '5433';
$database = 'sustainable';
$user = 'postgres';
$password = 'postgis';
$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>

Output displayed :

enter image description here

Output required is :

enter image description here

最佳答案

为什么不将该 php 代码放在不同的文件中?

然后调用它:

include('/my/phpfile.php')

关于javascript - 如何连接两个php代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49253158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com