1

I am very new to php. I have a simple website that loops through a mysql database and draws polygons for every row. It also assigns a custom attribute (name) to every polygon that the user can see when he clicks the polygon.

When a user clicks the polygon, he should see the value of variable "bname"

The code works perfectly when I assign bname to a coordinate:

var bname = ('.$row[coord1].');

The code fails when I assign bname to another column that is a string:

var bname = ('.$row[name].');

All data in the database is text format, I suspect that maybe I am using the wrong syntax to return a string value from the database? I need to assign bname to the name column in the database..

This is the full code:

$findmap= 'SELECT * FROM build2';

if(!$result = $con->query($findmap)){
die('There was an error running the query 
    [' . $con->error . ']');
} else {
while ($row = $result->fetch_assoc()) {

  echo '        
        var polypath = [new google.maps.LatLng('.$row[coord1].'),
                new google.maps.LatLng('.$row[coord2].'),
                new google.maps.LatLng('.$row[coord3].'),
                new google.maps.LatLng('.$row[coord4].'),
                new google.maps.LatLng('.$row[coord5].'),
                new google.maps.LatLng('.$row[coord6].'),
                new google.maps.LatLng('.$row[coord1].')
                ];


        var bname = ('.$row[name].');

        var dxbmap = new google.maps.Polygon({
        custom: bname,
        path:polypath,
        strokeColor:"#0000FF",
        strokeOpacity:0.8,
        strokeWeight:1,
        fillColor:"#0000FF",
        fillOpacity:0.1

        });

        dxbmap.setMap(map);

        google.maps.event.addListener(dxbmap, "click", function() {
        alert(this.custom);

    });

';
}
}
?>
6
  • Try like :- var bname = ("$row['name']"); Or var bname = ($row['name']); and check. Commented Jun 14, 2015 at 18:35
  • If it is a string I think it needs to be in quotes. Check the developer console, what is the error being thrown? Commented Jun 14, 2015 at 18:49
  • If I understand your question, then you need to add <script> tag before "var polypath" as well close it after "});" and for PHP db variable you should use like $row["coord2"] instead of $row[coord2] Commented Jun 14, 2015 at 19:03
  • Thanks for the quick response, unfortunately however the adjustments did not resolve the problem with the variable. @anant -both adjustments didn't work, it only works with the "." as in the code I shared above. Commented Jun 15, 2015 at 17:37
  • @ghanshyam -Thanks but It didnt work, however please note that when I fetch the "coord" column it works fine. the issue is only when fetching the "name" column from the DB. Commented Jun 15, 2015 at 17:42

1 Answer 1

1

You need to check your console in the browser for JS errors which I think is what you are encountering. Try this

$findmap= 'SELECT * FROM build2';

if(!$result = $con->query($findmap)){
die('There was an error running the query 
    [' . $con->error . ']');
} else {
while ($row = $result->fetch_assoc()) {

  echo '        
        var polypath = [new google.maps.LatLng('.$row[coord1].'),
                new google.maps.LatLng('.$row[coord2].'),
                new google.maps.LatLng('.$row[coord3].'),
                new google.maps.LatLng('.$row[coord4].'),
                new google.maps.LatLng('.$row[coord5].'),
                new google.maps.LatLng('.$row[coord6].'),
                new google.maps.LatLng('.$row[coord1].')
                ];


        var bname = \''.$row[name].'\';

        var dxbmap = new google.maps.Polygon({
        custom: bname,
        path:polypath,
        strokeColor:"#0000FF",
        strokeOpacity:0.8,
        strokeWeight:1,
        fillColor:"#0000FF",
        fillOpacity:0.1

        });

        dxbmap.setMap(map);

        google.maps.event.addListener(dxbmap, "click", function() {
        alert(this.custom);

    });

';
}
}
?>

With your current code your console log I think would be displaying something like

Uncaught ReferenceError:

The parenthesis don't encapsulate a string, is that what you were trying to do with those?

Sign up to request clarification or add additional context in comments.

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.