weixin_33747129 2018-03-13 09:59 采纳率: 0%
浏览 93

如何连接两个PHP代码

This is my code to select the location from dropdown menu and to draw pie chart from the postgresql database. I am not able to draw pie chart on selecting the specific location instead on selecting a location pie chart is appearing which contains all the values of a columns.

Database to create pie chart :

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

  • 写回答

2条回答 默认 最新

  • ~Onlooker 2018-03-13 10:31
    关注

    Why don't you put that php code in a different file?

    And then call it:

    include('/my/phpfile.php')
    
    评论

报告相同问题?