/    Sign up×
Community /Pin to ProfileBookmark

Class Question

I have the following class:

[code=php]
class zipcode_class {

var $last_error = “”;
var $last_time = 0;
var $units = _UNIT_MILES;
var $decimals = 2;

function get_zip_point($zip) {

$sql = “SELECT lat, lon from zip_code WHERE zip_code=’$zip'”;
$r = mysql_query($sql);
if (!$r) {
$this->last_error = mysql_error();
return false;
}

$row = mysql_fetch_array($r);
if(mysql_num_rows($r) == 0) {
$mess = “Zip Code Not Found”;
return $mess;
} else {
mysql_free_result($r);
return $row;
}
}

function get_zips_in_range($zip, $range, $sort, $include_base) {

$this->chronometer();

$details = $this->get_zip_point($zip);
if ($details == false) return false;

$lat_range = $range/69.172;
$lon_range = abs($range/(cos($details[0]) * 69.172));
$min_lat = number_format($details[0] – $lat_range, “4”, “.”, “”);
$max_lat = number_format($details[0] + $lat_range, “4”, “.”, “”);
$min_lon = number_format($details[1] – $lon_range, “4”, “.”, “”);
$max_lon = number_format($details[1] + $lon_range, “4”, “.”, “”);

$return = array(); // declared here for scope

$r = mysql_query(“SELECT zip_code, lat, lon FROM zip_code WHERE lat BETWEEN ‘$min_lat’ AND ‘$max_lat’ AND lon BETWEEN ‘$min_lon’ AND ‘$max_lon'”) or die(mysql_error());

while ($row = mysql_fetch_row($r)) {

$dist = $this->calculate_mileage($details[0],$row[1],$details[1],$row[2]);
if ($this->units == _UNIT_KILOMETERS) {
$dist = $dist * _M2KM_FACTOR;
}
if ($dist <= $range) {
$return[str_pad($row[0], 5, “0”, STR_PAD_LEFT)] = round($dist, $this->decimals);

}
}
mysql_free_result($r);

switch($sort) {

case _ZIPS_SORT_BY_DISTANCE_ASC:
asort($return);
break;

case _ZIPS_SORT_BY_DISTANCE_DESC:
arsort($return);
break;

case _ZIPS_SORT_BY_ZIP_ASC:
ksort($return);
break;

case _ZIPS_SORT_BY_ZIP_DESC:
krsort($return);
break;
}

$this->last_time = $this->chronometer();

if (empty($return)) return false;
return $return;
}
}
[/code]

And when I instantiate the class and call the function doing the following:

[code=php]
$zips = new zipcode_class();
$zips->get_zips_in_range($zip, $miles, _ZIPS_SORT_BY_DISTANCE_ASC, true);
[/code]

I cycle through the $zips array using a foreach loop and all I get as output are the names of the variables, not the values: last_error, last_time, units m, decimals 2

Am I instantiating the class wrong or handling the array wrong???

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 01.2010 — I didn't look over it in detail, but at first glance it looks like you need to assign the result of that method call to a variable:
[code=php]
$zips = new zipcode_class();
$zipsInRange = $zips->get_zips_in_range($zip, $miles, _ZIPS_SORT_BY_DISTANCE_ASC, true);
[/code]
Copy linkTweet thisAlerts:
@NtrimgsauthorApr 01.2010 — Thank You VERY Much!!! That was the problem!
×

Success!

Help @Ntrimgs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...