36 users online (0 members and 36 guests)  


  Results 1 to 3 of 3

Related

  1. Top 10 sorting    Forum: Javascript Forum
    Replies: 11
  2. Time sorting    Forum: Javascript Forum
    Replies: 1
  3. Sorting with perl    Forum: CGI Perl Forum
    Replies: 2
  4. Sorting alhpanumeric files    Forum: CGI Perl Forum
    Replies: 1
  5. Replies: 1
  1. #1
    yesti's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2008
    Posts
    2

    Sorting by column on webpage

    I have very little experience with perl. I have a plx/tmpl page that displays a data table based on a @records array. I am able to sort the array by a subkey.

    I want to create links out of the column headers then change the sort order based on what the user clicks (ascending/descending). The links change the 'sort' querystring value. I am able to get the querystring with:

    Code:
    my $sortvar = param('sort');
    I coded the tmpl file to change the link when the user clicks it, that part works fine.
    I tried this in my plx page but it does not do anything:

    Code:
    if ($sortvar == '1')
    {
    my @records = map  { $_->[0] }
                 sort { $b->[1] cmp $a->[1] }  
                 map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
                       my ($d,$m,$y) = split(/\s+/,$date);
                       $d = $d<10 ? "0$d" : $d;
                       $m = $month{lc substr $m,0,3};
                       [$_,"$y$m$d"]} @records;
    }
    elsif ($sortvar == '2')
    {
    my @records = map  { $_->[0] }
                 sort { $a->[1] cmp $b->[1] }  
                 map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
                       my ($d,$m,$y) = split(/\s+/,$date);
                       $d = $d<10 ? "0$d" : $d;
                       $m = $month{lc substr $m,0,3};
                       [$_,"$y$m$d"]} @records;
    }
    Any ideas?

  2. #2
    yesti's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2008
    Posts
    2

    Re: Sorting by column on webpage

    The problem: @records was scoped wrong. It was declared globally way above this code snippet so taking out the 'my' in front fixed the problem. As is, it was local to the if statement.

  3. #3
    HTML's Avatar
    Administrator

    Status
    Offline
    Join Date
    Aug 2000
    Posts
    3,445

    Follow HTML On Twitter Add HTML on Facebook Add HTML on Google+ Add HTML on Linkedin Visit HTML's Youtube Channel

    Re: Sorting by column on webpage

    Welcome to the forums

    I am glad you got it sorted out, I am sure your solution will be helpful to others down the road.

    Dave
    AHFBWEB Less customers per server, more power for you!

    Business Class Shared Hosting



Tags for this Thread