Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-20-2013, 04:22 PM   PM User | #1
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
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
sonny is offline   Reply With Quote
Old 02-20-2013, 06:17 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
Fou-Lu is offline   Reply With Quote
Old 02-21-2013, 06:20 AM   PM User | #3
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
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
sonny is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:41 PM.


Advertisement
Log in to turn off these ads.