Home : Products : Gossamer Links : Development, Plugins and Globals :

Products: Gossamer Links: Development, Plugins and Globals: Re: [rgbworld] Need help with Sort by distance: Edit Log

Here is the list of edits for this post
Re: [rgbworld] Need help with Sort by distance
Well here it is.. Everything works except Sort by distance
The gathering of link ids at or below a given category could probably be shortened, but I don't know how.

Snippet
Code:
my @ids;

my %filter = (
isValidated => 'Yes',
mh => $args->{mh},
nh => $args->{nh},
sb => $args->{sb},
so => $args->{so},
ww => 1
);

# Should not select unpaid and expired links if payment is enabled
$filter{ExpiryDate} = '>=' . time if $CFG->{payment}->{enabled};

if ($catid)
{
# This is just the short category name for display on template results
$catname = $categories_db->select( ['Name'], { ID => $catid } )->fetchrow;

# We want all links in catid and below. Found at GT forums under <%load_lower_links($ID)%>
my $catname_full = $categories_db->select( ['Full_Name'], { ID => $catid } )->fetchrow;
my $cond = GT::SQL::Condition->new(
Full_Name => '=' => "$catname_full",
Full_Name => 'LIKE' => "$catname_full/%"
);
$cond->bool('OR');
my $sth = $categories_db->select( $cond ) || die $GT::SQL::error;

my $count = '0';
while (my $hit = $sth->fetchrow_hashref) {
my $links_sth = $DB->table('CatLinks')->select( { CategoryID =>$hit->{ID} } ) || die $GT::SQL::error;

while (my $hit2 = $links_sth->fetchrow_hashref) {

my $link = $links_db->get($hit2->{LinkID});

# Get Current Links Zip Code from Main ZipCode DB
my $osth = $zipcode_db->select({ ZipCode => $link->{Zip} })->fetchrow_hashref;
my $currlat = $osth->{Latitude};
my $currlon = $osth->{Longitude};

# The actual distance search
$temp_distance = distance($olat, $olon, $currlat, $currlon, $unitcode);

if($temp_distance <= $dist)
{
$count++;
push (@ids, $link->{ID});
}
}
}

$filter{ID} = \@ids if $count > 0;
}
else
{
my @zips = matching_zips($query, $unitcode, $dist);
$filter{Zip} = \@zips;
}

my $link_sth = $links_db->query_sth(\%filter);
$link_count = $links_db->hits;

if ($link_count) {
my $results = $link_sth->fetchall_hashref;
.....
}

Sort by distance plays a weird role, especially if grouping by category.
Now I can sort by Zip or any other existing coulumn, and I still wonder if I can't just sort the zipcodes in order of 'distance' when gathering the array of zips.

See sub matching_zips earlier in this thread (here)

Thanks again for any help in finalizing this.
It is nearly perfect and can be seen in use here
http://www.supportmusicians.com/...cgi?p=zipcode_search

A good zip code to search around is 53142. Try 4 miles, 20 miles, 400 miles and watch the results increase!

Thanks
Chris
RGB World, Inc. - Software &amp; Web Development.
rgbworld.com

Last edited by:

rgbworld: May 29, 2005, 4:43 PM

Edit Log: