loading comma separated coordinate values into an array

I am building a script that will generate a map image based on coordinates it gets from a MySQL database. The coordinates are stored as comma separated values in a field in the database (i.e. “292,45,366,49,366,68,346,88,311,86” is stored in one field). The imagepolygon() expects the coordinates to be an array. I am using the following script, but I am getting an error that it is not an array:

$coords = array(mysql_result($result, $j, coordinates)); coordinates = “292,45,366,49,366,68,346,88,311,86”

I then load this into the imagepolygon() function like so:

imagepolygon($masterMap, $coords, $points, $color);

This produces the following error:
imagepolygon() [function.imagepolygon]: 2nd argument to imagepolygon not an array in /home/public_html/createMap.php on line 47

If I rewrite the function as follows, it works but is obviously not a workable solution:
imagepolygon($masterMap, array(292,45,366,49,366,68,346,88,311,86), $points, $color);

Any ideas?

Is that the only error you get? Shouldn’t coordinates be a string?

[php]
// coordinates is a constant
mysql_result($result, $j, coordinates);

// coordinates is a string
mysql_result($result, $j, ‘coordinates’);
[/php]

no other errors

Do this…

[php]
$res = mysql_result($result, $j, ‘coordinates’);
var_dump($res);
exit;
[/php]

What does it output?

string(63) “99,148,100,78,109,25,147,18,159,28,168,44,168,57,142,75,151,126”

So you would want to explode your string. For example

[php]
$res = mysql_result($result, $j, ‘coordinates’);
$coords = explode(’,’, $res);
imagepolygon($masterMap, $coords, $points, $color);
[/php]

Yes! That worked perfectly, thanks! :smiley:

No problem. You should probably take it one step further and convert all values to integers just to be safe.

[php]
$coords = array_map(‘intval’, explode(’,’, $res));
[/php]

OK, will do

Sponsor our Newsletter | Privacy Policy | Terms of Service