hi <br>I have a table in postgresql that it has topology and I want to display it as line in openlayers.<br>I use php to connect and convert my data to Geojson. this the codes.but I can not get the result.who can help me.where is wrong in my codes,this is the code.<br>
<br><br><span style="color: rgb(204, 0, 0);"><?php</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> // Database connection settings</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> define("PG_DB" , "tehran");</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> define("PG_HOST", "localhost"); </span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> define("PG_USER", "postgres");</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> define("PG_PORT", "5432"); </span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> define("TABLE", "close");</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> </span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">?></span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"><?php</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">// Connect to database</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER);</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> $sql = "SELECT gid, ST_AsGeoJSON(the_geom) AS geojson, length(the_geom) AS length FROM ".TABLE.""</span><br style="color: rgb(204, 0, 0);">
<br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> // Perform database query</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> $query = pg_query($dbcon,$sql); </span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> </span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">?></span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"><?php</span><br style="color: rgb(204, 0, 0);">
<br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> // Return route as GeoJSON</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> $geojson = array(</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> 'type' => 'FeatureCollection',</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> 'features' => array()</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> ); </span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> </span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> // Add edges to GeoJSON array</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> while($edge=pg_fetch_assoc($query)) { </span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> $feature = array(</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> 'type' => 'Feature',</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> 'geometry' => json_decode($edge['geojson'], true),</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> 'crs' => array(</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> 'type' => 'EPSG',</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> 'properties' => array('code' => '4326')</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> ),</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> 'properties' => array(</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> 'id' => $edge['id'],</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> 'length' => $edge['length']</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> )</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> );</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> </span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> // Add feature array to feature collection array</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> array_push($geojson['features'], $feature);</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> }</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"></span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> // Close database connection</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> pg_close($dbcon);</span><br style="color: rgb(204, 0, 0);"><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> // Return routing result</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> header('Content-type: application/json',true);</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);"> echo json_encode($geojson);</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);"> </span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">?></span><br>
<br>