2

So, I have a postgresql database and I'm querying a particular column's values and assigning each value to a javascript array. Now I have a javascript function which outputs this data on a map. When i test the array manually (Entering the respective array index one by one), it returns the output perfectly alright. But in a loop (When i want to output all at once), i can only see the first element's output. The rest of the array is simply ignored or there's an error. I don't understand where the mistake is. Here's the code:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyC0HZ81Ea8Jy8fJ6zm6_uvPp8UhLg5mczo&sensor=true"></script>
<script type="text/javascript" src="GeoJSON.js"></script>
<script type="text/javascript" src="polygon.js"></script>


<script type="text/javascript">

var geojson_Polygon = new Array();

function kaushik()
{

    <?php
        require("header.php");

        $connection = pg_connect($full);

        if(!$connection)
        {
            pg_close($connection);

        }

        $result = pg_query($connection, "SELECT ogc_fid, ST_AsGeoJSON(wkb_geometry), name FROM features");
        if (!$result) 
        {
            exit;
        }

        $i = 0;         
        while ($row = pg_fetch_row($result)) 
        {   
            echo "\n";
            echo "geojson_Polygon[$i] = $row[1];";
            echo "\n";
            echo "\n";
            $i++;
        }

    ?>

    for (var j=0; j<317; j++)
    {
        showFeature(geojson_Polygon[j]);
    }


}
</script>
5
  • I don't know, what you want to do... You are generating PHP code with JS? This couldn't work i think, because PHP is a server side language and will be rendered on the server. JS runs on the client and will be rendered after PHP. Commented Apr 20, 2012 at 8:15
  • I'm sure it works. Cause the first iteration of the javascript loop is successful. Its only the successive iterations that fail. Commented Apr 20, 2012 at 8:28
  • @Tobi rendering javascript in PHP function perfectly. Commented Apr 20, 2012 at 8:28
  • @Nicola: can you explain, why this works? :-O Commented Apr 20, 2012 at 8:29
  • 1
    @Tobi PHP is processed on the server and then sent to the client, while javascript is processed on the client. Basically writing PHP code that echoes javascript or writing javascript code is exactly the same thing. Commented Apr 20, 2012 at 8:32

3 Answers 3

2

If what you want to do is build a JS array , then just do:

<script type="text/javascript">
   var data = [ <?php

      // --- snip
      $first = true;
      while ($row = pg_fetch_row($result)) 
      {
          if ( $first )
          {
             $first = false;
          }else{
             echo ',';
          }
          echo $row[$i]
      }
      // ---- snip

   ?> ];
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

The array builds fine. Now, when i try to output the array is the problem. I don't know where I'm going wrong there..
@KaushikTD , well . what is the output you get ?
snipplr.com/view/64689/javascript-array-assigned-using-php-loop
What I meant was that the array builds fine with the code I gave. I believe there's some problem when pulling out all the data at one shot.
0

i think you should do

 while ($row = pg_fetch_row($result)) 
    {   
        echo "\n";
        echo "geojson_Polygon[$i] = $row[$i];";
        echo "\n";
        echo "\n";
        $i++;
    }

otherwise the value is always the same

6 Comments

okay so showFeature(); is a javascript function found in polygon.js which plots the polygon on a map (after converting from GeoJSON to Google Maps Api format). This is successful. the conversion and plotting. Please see that the for loop falls only after the php end tag. I'm using php and javascript. Php to query the db. And I'm using echo to output javascript.
@KaushikTD maybe you should change $row[1] to $row[$i] otherwise in the loop $row[1] is always the same. Anyway can you post the generated html?
in PostGresql row[1] refers to the first column and not the first row! The php loop automatically queries all the data in that column row[1]. If I did what you've mentioned, I'll be getting results from all the columns, and not the particular column i want data out of.
@KaushikTD yep you are right, i'll delete this, can you please post the generated javascript?
The generated javascript is pretty long, about 317 data points, with each point holding co-ordinates of a polygon in GeoJSON format. I'll link it to you. Just give me a sec.
|
0

Just read the rows into a php array and then assign it as json.

<script>
...
var geojson_Polygon = <?php echo json_encode($all_rows); ?>;
....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.