Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#1 sort shipping methods by price

open
nobody
None
5
2014-09-04
2001-05-10
robert chou
No

in state.pm, starting at line 1529, replace the
following code:

## now get costs for each available method
foreach my $i ( 0 .. $#methods ) {
my ( $meth,$zone ) = split /\t/, $methods[$i];
$q = "SELECT * from zonerate
WHERE shipmeth='$meth' and
shipzone='$zone' and shipwt>='$weight'
ORDER BY shipwt ";
warn "Q is $q\n";
$sth = $dbh->prepare($q);
$sth->execute();
my $h = $sth->fetchrow_hashref('NAME_lc');
if ( $h ) { # just need the first returned
record
$methods[$i] = join "\t", ( $meth, $h->
{'shipcost'}, $titles{$meth} );
} else { # this isn't available
for here
$methods[$i]="ERROR - method not available
for this location";
} # done with decision on results

} # done getting costs
## disconnect and cleanup
#
$sth->finish() if ( $sth );

my $error = 1;
foreach my $method (@methods) {
$error = 0 if ($method !~ /^ERROR/);
}

if ($error == 1) {
return @methods;
}

# cleanup error rows from list
my @temp=();
while ( @methods ) {
my $l = shift @methods;
next if ( $l =~ /^ERROR/ );
push @temp, ( $l );
}
@methods=@temp;

---

with the following:

## now get costs for each available method
my %forsort = ();
foreach my $i ( 0 .. $#methods ) {
my ( $meth,$zone ) = split /\t/, $methods[$i];
$q = "SELECT * from zonerate
WHERE shipmeth='$meth' and
shipzone='$zone' and shipwt>='$weight'
ORDER BY shipwt ";
warn "Q is $q\n";
$sth = $dbh->prepare($q);
$sth->execute();
my $h = $sth->fetchrow_hashref('NAME_lc');
if ( $h ) { # just need the first returned
record
$methods[$i] = join "\t", ( $meth, $h->
{'shipcost'}, $titles{$meth} );

$forsort{$i} = $h->{'shipcost'};
} else { # this isn't available
for here
$methods[$i]="ERROR - method not available
for this location";

$forsort{$i} = '0.00';
} # done with decision on results

} # done getting costs
## disconnect and cleanup
#
$sth->finish() if ( $sth );

my $error = 1;
foreach my $method (@methods) {
$error = 0 if ($method !~ /^ERROR/);
}

if ($error == 1) {
return @methods;
}

# sort results
my @temp=();
my @sortedKeys = sort { $forsort{$a} <=> $forsort
{$b} } keys(%forsort);
foreach( @sortedKeys ) {
my $l = $methods[$_];
push @temp, ( $l );
}
@methods=@temp;

# cleanup error rows from list
my @temp=();
while ( @methods ) {
my $l = shift @methods;
next if ( $l =~ /^ERROR/ );
push @temp, ( $l );
}
@methods=@temp;

Discussion

  • Logged In: NO

    It seems easy to just replace the
    ORDER BY shipmeth
    with
    ORDER BY objclass

    It puts it in the proper order, even though the first one is
    Ground Residential followed by Ground Commercial