469,307 Members | 2,110 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

Sorting by column on webpage

4
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:

Expand|Select|Wrap|Line Numbers
  1. my $sortvar = param('sort');
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. if ($sortvar == '1')
  2. {
  3. my @records = map  { $_->[0] }
  4.              sort { $b->[1] cmp $a->[1] }  
  5.              map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
  6.                    my ($d,$m,$y) = split(/\s+/,$date);
  7.                    $d = $d<10 ? "0$d" : $d;
  8.                    $m = $month{lc substr $m,0,3};
  9.                    [$_,"$y$m$d"]} @records;
  10. }
  11. elsif ($sortvar == '2')
  12. {
  13. my @records = map  { $_->[0] }
  14.              sort { $a->[1] cmp $b->[1] }  
  15.              map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
  16.                    my ($d,$m,$y) = split(/\s+/,$date);
  17.                    $d = $d<10 ? "0$d" : $d;
  18.                    $m = $month{lc substr $m,0,3};
  19.                    [$_,"$y$m$d"]} @records;
  20. }
  21.  
Any ideas?
Feb 5 '08 #1
6 1112
KevinADC
4,059 Expert 2GB
Giving up on Tek-Tips? ;)
Feb 5 '08 #2
yesti
4
Giving up on Tek-Tips? ;)
The tek-tips site was giving me errors earlier, so I had to search for a few more forums. I'm starting to really not like perl :(
Feb 5 '08 #3
KevinADC
4,059 Expert 2GB
The tek-tips site was giving me errors earlier, so I had to search for a few more forums. I'm starting to really not like perl :(

Yea, Tek-Tips does that more than most websites, errors that is.

Not liking perl? May a foul wind blow in your general direction! ;)
Feb 5 '08 #4
yesti
4
LOL. Well many thanks to people like you that are making the experience a little better.

I see you replied to my topic on tek-tips. Waiting for that site to respond is killing me ;-P
Feb 5 '08 #5
KevinADC
4,059 Expert 2GB
LOL. Well many thanks to people like you that are making the experience a little better.

I see you replied to my topic on tek-tips. Waiting for that site to respond is killing me ;-P
In case you can't open TT:

@records is scoped wrong. When you use "my", which you should, the variables is only visible to the enclosing block, in your case the if{} elsif{} blocks. Also you don't need to quote a number to match it numerically.

Expand|Select|Wrap|Line Numbers
  1. my @sorted;#<--scoped globally
  2. if ($sortvar == 1)
  3. {
  4. @sorted = map  { $_->[0] }
  5.              sort { $b->[1] cmp $a->[1] }  
  6.              map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
  7.                    my ($d,$m,$y) = split(/\s+/,$date);
  8.                    $d = $d<10 ? "0$d" : $d;
  9.                    $m = $month{lc substr $m,0,3};
  10.                    [$_,"$y$m$d"]} @records;
  11. }
  12. else {#<-- you don't really need to check the value of $sortvar here becuase if it is not 1 then this is the default sort.
  13. @sorted = map  { $_->[0] }
  14.              sort { $a->[1] cmp $b->[1] }  
  15.              map  {(my $date = $_->{COMPDATE}) =~ s/<font color="#FF0000">//;
  16.                    my ($d,$m,$y) = split(/\s+/,$date);
  17.                    $d = $d<10 ? "0$d" : $d;
  18.                    $m = $month{lc substr $m,0,3};
  19.                    [$_,"$y$m$d"]} @records;
  20. }
  21. print "$_\n" for @sorted; 
Feb 6 '08 #6
yesti
4
TT just came up before I posted this, thanks!
Feb 6 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by rkbnair | last post: by
19 posts views Thread by Owen T. Soroke | last post: by
2 posts views Thread by jediknight | last post: by
4 posts views Thread by Ambica Jain | last post: by
7 posts views Thread by Kamal | last post: by
5 posts views Thread by jrod11 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.