Enjoy an ad free experience by logging in. Not a member yet?
Register .
02-20-2013, 04:22 PM
PM User |
#1
Regular Coder
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
foreach proper use of key values?
Hi, I'm creating a location map, this works for multi values from db.
PHP Code:
while( $row = mysql_fetch_array ( $result )) {
$lat = $row [ 'lat' ];
$lon = $row [ 'lon' ];
$pt = getlocationcoords ( $lat , $lon , $scale_x , $scale_y );
imagefilledrectangle ( $im , $pt [ "x" ]- 2 , $pt [ "y" ]- 2 , $pt [ "x" ]+ 2 , $pt [ "y" ]+ 2 , $dotcolor );
}
problem is I need to do this with 2 pairs of lat and lon with a foreach
not sure of the format how to do this or how to use the key/s properly
PHP Code:
$cord ;
$cord [ "lat1" ] = $lat1 ;
$cord [ "lon1" ] = $lon1
$cord [ "dotcolor1" ] = $dotcolor1 ;
$cord [ "lat2" ] = $lat2 ;
$cord [ "lon2" ] = $lon2
$cord [ "dotcolor2" ] = $dotcolor2 ;
foreach( $cord as $key => $value ){
//$pt = getlocationcoords($lat, $lon, $scale_x, $scale_y);
//imagefilledrectangle($im,$pt["x"]-2,$pt["y"]-2,$pt["x"]+2,$pt["y"]+2,$dotcolor);
}
Thanks
Sonny
02-20-2013, 06:17 PM
PM User |
#2
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
How did you get $lat1/$lon1 and $lat2/$lon2 to start with?
In order to get what you want in pairs, you need to specify them as an array within the single offset of $cord.
PHP Code:
$cord = array( array( 'lat' => array( $lat1 , $lat2 ), 'lon' => array( $lon1 , $lon2 ), 'dotcolor' => array( $dotcolor1 , $dotcolor2 ) ) ); foreach ( $cord AS $itm ) { list( $firstLat , $secondLat ) = $itm [ 'lat' ]; list( $firstLon , $secondLon ) = $itm [ 'lon' ]; list( $firstDotColour , $secondDotColour ) = $itm [ 'dotcolor' ]; printf ( '$firstLat: %s, $secondLat: %s, $firstLon: %s, $secondLon: %s, $firstDotColour: %s, $secondDotColour: %s' , $firstLat , $secondLat , $firstLon , $secondLon , $firstDotColour , $secondDotColour ); }
That's for if you want to inter-relate multiple things together.
For individuals at a time, you can use like so:
PHP Code:
$cord [ 0 ][ 'lat' ] = $lat1 ; $cord [ 0 ][ 'lon' ] = $lon1 ; $cord [ 0 ][ 'dotcolor' ] = $dotcolor1 ; $cord [ 1 ][ 'lat' ] = $lat2 ; $cord [ 1 ][ 'lon' ] = $lon2 ; $cord [ 1 ][ 'dotcolor' ] = $dotcolor2 ;
Or just traditional array building with key=>value pairs. Iteration would then have $value as an array with lat, lon, and dotcolor as valid associative indexes.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer:
http://php.net/manual/en/mysqlinfo.api.choosing.php
02-21-2013, 06:20 AM
PM User |
#3
Regular Coder
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
Hi, Fou
Wound up trying two arrays with a combine "just a 3 line foreach", which did work, but then
realized I needed a separate dot color as well so I finally just did this.
PHP Code:
$pt2 = getlocationcoords ( $lat2 , $lon2 , $scale_x , $scale_y );
imagefilledrectangle ( $im , $pt2 [ "x" ]- 2 , $pt2 [ "y" ]- 2 , $pt2 [ "x" ]+ 2 , $pt2 [ "y" ]+ 2 , $dotcolor2 );
and all was fine
Sonny
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 11:41 PM .
Advertisement
Log in to turn off these ads.