如何连接两个PHP代码

时间:2018-03-13 09:59:01

标签: javascript php html ajax postgresql

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

创建饼图的数据库:

enter image description here

  

选择状态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>
  

显示输出:

enter image description here

  

所需的输出是:

enter image description here

2 个答案:

答案 0 :(得分:3)

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

然后叫它:

include('/my/phpfile.php')

答案 1 :(得分:3)

require一次代码允许您连接不同目录和同一目录中的代码。 点代表它在同一目录中。 这是另一个目录的代码:

require_once("../includes/database.php");

这是同一目录的代码。

require_once("database.php");

我认为这可能是您正在寻找的一种方式,也是最好,最有条理的方式。 所以我建议将它放在一个不同的php文件中,并使用此代码示例链接它们。

注意在我的情况下,该文件名为database.php