1

I am using an example fracnsites to make a similar map but I don't under stand the ajax code? The example has js code:

var map,
fields = ["gid", "createdby", "featname", "feattype", "status", "acres"], 
autocomplete = [];
function getData(){
$.ajax("php/getData.php", {
    data: {
        table: "fracsandsites",
        fields: fields
    },
    success: function(data){
        mapData(data);
    }
})

};

function mapData(data){ //remove existing map layers map.eachLayer(function(layer){ //if not the tile layer if (typeof layer._url === "undefined"){ map.removeLayer(layer); } });

Here is the getdata php code:

<?php
ini_set('display_errors', 1); 

//database login info
$host = 'localhost';
$port = '5432';
$dbname = 'sandwikimap2';
$user = 'postgres';
$password = 'postgis';

$conn = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
if (!$conn) {
    echo "Not connected : " . pg_error();
    exit;
}

//get the table and fields data
$table = $_GET['table'];
$fields = $_GET['fields'];

//turn fields array into formatted string
$fieldstr = "";
foreach ($fields as $i => $field){
    $fieldstr = $fieldstr . "l.$field, ";
}

//get the geometry as geojson in WGS84
$fieldstr = $fieldstr . "ST_AsGeoJSON(ST_Transform(l.geom,4326))";

//create basic sql statement
$sql = "SELECT $fieldstr FROM $table l";

//if a query, add those to the sql statement
if (isset($_GET['featname'])){
    $featname = $_GET['featname'];
    $distance = $_GET['distance'] * 1000; //change km to meters

    //join for spatial query - table geom is in EPSG:26916
    $sql = $sql . " LEFT JOIN $table r ON ST_DWithin(l.geom, r.geom, $distance) WHERE r.featname = '$featname';";
}

Here is my code:

    <?php

   //database login info
$host = 'localhost';
$port = '5432';
$dbname = 'thndr';
$user = 'postgres';
$password = 'somepassword';

// connect to database
$conn = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
if (!$conn) {
    echo "Not connected : " . pg_error();
    exit;
}
//get the data
$propBoundaries = $_GET['tm4326'];
$flagPoints = $_GET['tmxy4326'];

//turn propertyBoundaries array into formatted string
$propBoundaries = "";
foreach ($propBoundaries as $i => $propBoundary){
    $propBoundaries = $propBoundaries . "l.$propBoundary, ";
}

//get the geometry as geojson in WGS84
$propBoundaryDatastr = $propBoundarystr . "ST_AsGeoJSON(ST_Transform(l.geom,4326))";

//turn PropertyPoints array into formatted string
$flagPoints = "";
foreach ($flagPoints as $i => $flagPoint){
    $flagPoints = $flagPoints . "l.$flagPoint, ";
}

//get the geometry as geojson in WGS84
$flagPoints = $flagPoints . "ST_AsGeoJSON(ST_Transform(l.geom,4326))";

//create basic sql statement
$sql = "SELECT *
from $propBoundaries.l 
inner join $flagPoints.l
on $propBoundaries.gid=$flagPoints.gid";

echo $sql;

//send the query
if (!$response = pg_query($conn, $sql)) {
    echo "A query error occured.\n";
    exit;
}

//echo the data back to the DOM
while ($row = pg_fetch_row($response)) {
    foreach ($row as $i => $attr){
        echo $attr.", ";
    }
    echo ";";
}

?>

I don't understand what the ajax call in the example is actually doing? Therefore, I can't write a get data php file to connect to my database. When the ajax in the example says data: { table: "fracsandsites", fields: fields What exactly is table:? is that some preset thing in ajax? I am trying to convert my map which runs from a geojson file to coming from a postgis database instead. Here is what I have so far, but honestly I don't understand what I am doing.

-1

table: "fracsandsites" is table name of DB eg $dbname = 'sandwikimap'

  • Could you explain more what you mean? – BERA Mar 8 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.