eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Development resources
 ::   WebWare 2.1 (Perl)
 ::   WebWare 2.0 (Java)
 ::   Cool hacks
radical hacks
WebStore Sorting  
sent in the following rad hack.....

Hi guys just added a new functionality to the web_store script That I thought you might want to show folks.

I'm doing two separate implementations of the script that require things be displayed sorted by date. I looked through the forum and saw I wasn't the only one wanting to do this so I took it upon myself to try figuring out the ever so cryptic sort command. The result is actually usable for any type of field you might wish to sort by. It's not the cleanest code in the world but it works.

step 1.) add this to the end of the web_store.cgi file:

#######################################
# -JV- presort results I've added this little
# snippet to allow results 
# of a search to be sorted by the start date
# of the particular event
# This of course can be adjusted to sort by whatever
# you wish and even 
# added to the search criteria with out too much
# trouble. Of course you 
# also need to add the date_to_unix subroutine to
# get this to actually 
# work for date sorting.
##############################################
sub get_this_sorted {

@data_sorted = @database_rows;

        foreach (@data_sorted) {
                ($item_num,$type,$price,$start,$end,
                 $locat,$name,$pictur,$desc,$options,
                 $avail) = split /\|/; # get start
              $start{$_} = $start; # record it
        }
    @database_rows = sort {
               &date_to_unix($start{$a}) <=>
               &date_to_unix($start{$b});
        } @data_sorted;
        }
        
sub date_to_unix {
#####################################
# This routine just takes your date
# format and return the  UNIX time().
#####################################
    my ($date)   = $_[0]; 
    my (%months) = ("Jan" => 0, "Feb" => 1, "Mar" => 2,
                    "Apr" => 3, "May" => 4, "Jun" => 5, 
                    "Jul" => 6, "Aug" => 7, "Sep" => 8,
                    "Oct" => 9, "Nov" => 10,"Dec" => 11);
 my ($time);
    my ($day, $mon, $year) = split(/-/, $_[0]);
    unless ($day and $mon and $year)  { return undef; }
    unless (defined($months{$mon}))   { return undef; }    

 use Time::Local;
    eval {  
  $day = int($day); $year = int($year) - 1900; 
        $time = timelocal(0,0,0,$day, $months{$mon}, $year);
    };
    if ($@) { return undef; } # Could return 0 if you want.
    return ($time); 
}

Step 2.) call the get_this_sorted routine from the subroutine create_html_page_from_db in web_store.cgi somewhere around line 1928 just before the foreach statement used on @database_rows. That's it! It can be customized to sort by whatever field you want and can even be added to the search criteria with a simple if statement. With all the brain damage I went through to get this figured out, I thought I'd try to spare others that fate. As soon as I get these projects done (they were supposed to be done yesterday), I'll do a better job of integrating it by putting a couple of variables into the setup file.