Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Category Sort Order (@Related)

Quote Reply
Category Sort Order (@Related)
Hi,

I've been puzzling over the problem of sorting categories that include '@' related subcats.

The solution I've come up with is to re-sort $display{category_loop} by the field Name before passing it to the $display{category} sub.

How would I write a statement to re-sort it by Name?

All the best
Shaun
Quote Reply
Re: [qango] Category Sort Order (@Related) In reply to
I've been wondering about the same thing.

http://www.gossamer-threads.com/...orum.cgi?post=203905

I don't think I ever got around to re-posting it in this forum...

Dan
Quote Reply
Re: [Dan Kaplan] Category Sort Order (@Related) In reply to
Okay, I've come up with a solution of sorts but it needs fixing.

The related names are applied after the list of subcats has been sorted (they're sorted using their actual name from the category table).

Once the related names have been applied, all we need to do is re-sort the array of hashes, and I've come up with the following to do that:

Code:
my $sort_data = $display{category_loop};
foreach my $hashref ( sort {$$a{Name} cmp $$b{Name}} @{$sort_data}) {
if ($hashref->{Name} eq $display{Name}) {
next;
}
else {
push @{$display{category_sorted_loop}}, $hashref;
}
}

The if removes a category link to itself that seems to appear after the sort?

Maybe I haven't quite got the sort statement right? (I'm trying to re-sort the array of hashes based on the 'Name' value in each hash)

Anyway, it works fairly well, but it chops the first category name. The first one is present in $display{category_loop}, but it disappears after the sort and is not there in $display{category_sorted_loop}.

Any ideas what is going wrong anyone?

PS. Would it be worth posting this into the Perl Discussion forum?

All the best
Shaun

NOTE: If you want to see the output, take a look at:

http://www.qango.com/dir/Business_and_Economy/Shopping_and_Services/index.html
Quote Reply
Re: [qango] Category Sort Order (@Related) In reply to
I think I've got this fixed now, so here's the actual mod to reorder subcategories (containing renamed related cats) into the correct alphabetical order.

In build.pm under sub build_category find
# Get the subcategories and related categories as either Yahoo style (integrated) or
# separated into two outputs..
:

Add the following, line after $display{category_loop} = [$category];

Code:
$display{category_sorted_loop} = [$category];


In the 'while' statement a little further down, update it by adding the new line as follows:

Code:
while (my $cat = $sth->fetchrow_hashref) {
if (exists $rel{$cat->{ID}}) {
$cat->{Related} = 1;
$cat->{Name} = $rel{$cat->{ID}};
}
push @{$display{category_loop}}, $cat;
push @{$display{sort_data}}, $cat;
}


After the 'while' statement, add the following:

Code:
# Take the current category_loop array of hashes and reorder by the value of the 'Name' field
foreach my $hashref ( sort {$$a{Name} cmp $$b{Name}} @{$display{sort_data}}) {
push @{$display{category_sorted_loop}}, $hashref;
}


Now update the category display line by adding the new sorted subcategory display, as follows:

Code:
$display{category} = Links::SiteHTML::display ('print_cat', $display{category_loop});
$display{categorysorted} = Links::SiteHTML::display ('print_cat', $display{category_sorted_loop});

This adds a new variable <%categorysorted%> for use in your category template. Simply replace <%category%> with <%categorysorted%> in your template to see the resulting re-sorted subcategory table.

Notes:
  • A cavaet is that you MUST give each related category a name.


  • If you've renamed your 'Name' field in the Category table, you'll need to adjust the code to suit the field name you've used. Adjust it at the sort in the foreach statement where you see $$a{Name} $$b{Name}


  • I've maintained the original <%category%> display variable as a backup, just in case I haven't got this mod quite right. If you need to, you can just reinsert <%category%> to get your old subcategory tables back.


  • AFAIK it works for both static and dynamic builds, although I'm not sure how much of a performance difference there will be on a large number of subcategories.


  • Hope this helps.

    All the best
    Shaun

    Last edited by:

    qango: Aug 12, 2002, 1:00 AM
    Quote Reply
    Re: [qango] Category Sort Order (@Related) In reply to
    Make sure you use lc() around the hashref keys in the sort bracket otherwise it may not sort as you expect.

    eg...

    lc($$b{Name})

    or

    lc($b->{Name})

    ...as I prefer Smile

    Last edited by:

    Paul: Aug 12, 2002, 2:20 AM
    Quote Reply
    Re: [Paul] Category Sort Order (@Related) In reply to
    Paul,

    Thanks for the advice.

    I'm not sure I've got it working 100% correct, as it does require a related name to be present for related subcats (else it displays the @ on its own), but its pretty much a usable solution for anyone who names each of their cat-relations.

    I'd been racking my brain for a solution, since the initial sort order is defined by a related-cat's parent in the database - the actual 'Related Name' is only transposed AFTER the sort, so that's why it was displaying out of order.

    The idea I came up with was to re-sort the array of hashes once the related name had been applied (the 'while' statement), however I do feel it could be improved and made perfect by anyone who really knows what they're doing - I've only cobbled it together from different perl discussions, etc. because I wanted it working alphabetically correct Smile

    If anyone does manage to perfect this mod, please let me know.

    All the best
    Shaun
    Quote Reply
    Re: [qango] Category Sort Order (@Related) In reply to
    I believe I've got a working version in place now for the PHP front end:

    http://www.gossamer-threads.com/...orum.cgi?post=203905

    It obviously won't carry over directly to the Perl version, but maybe it'll help somewhere in the process?

    Dan