Gossamer Forum
Home : Products : Links 2.0 : Customization :

Tree list by category

Quote Reply
Tree list by category
I'm looking for a variation of the tree mod, or at least any hints on how to create it.

What I'd like to do is have a normal homepage showing categories only, then one page for each category showing all links for that category and it's sub-categories. Optionally all the pages could be in the same directory rather than seperate sub-directories although that's not as important.

The directory I'm building will only have a relatively small number of links so having a seperate page/directory for a sub-category with say 2 links is a bit overkill.
Quote Reply
Re: [redshoes] Tree list by category In reply to
I think an easier solution would be to crete a second home page, as links already displays each category and subcategories on one page.

If you create a second homepage and edit the new sub just to show categories. I have this on my website, if this is what you require let me know. I have the main cats in a column on the left hand side and then the a second homepage in a Iframe on the right below the image. I do not use sub cats but they could easily be added in. Take a look at my site and tell me is this what you require

http://www.ultimateworld.info
Quote Reply
Re: Tree list by category In reply to
That's not quite what I'm after.

Home page can be a list of categories as normal
Quote:
  • Category 1
  • Category 2
  • Category 3


Each category page then shows links for that category and all categories under it
Quote:
Category 1
  • Link 1
  • Link 2

Category 1 > Sub-category 1
  • Link 1
  • Link 2
Category 1 > Sub-category 2
  • Link 1
  • Link 2


In other words pages are only built one directory level down.
Quote Reply
Re: [redshoes] Tree list by category In reply to
Maybe what would work for you is the infamous "Yahoo" mod, which lists clickable subcats ubder the main cats on the home page. Then, when a person clicks on the subcat, they go to a page with the actual links. See the second site in my signature for an example...


Leonard
aka PerlFlunkie
Quote Reply
Re: Tree list by category In reply to
Thanks guys, not quite what I was after but it did give me a few ideas. Looking through the forum this has come up a couple of times without a real answer. By 'borrowing' a few ideas from various places I've come up with the following. It's not exactly pretty but seems to work. Perl isn't my first language so be gentle with me ;)

In nph-build.cgi replace the build_category_pages sub with the following

Code:

sub build_category_pages {
# --------------------------------------------------------
# This routine builds all the category pages. Each category uses
# the same template which is defined in &site_html_category.

my $build_single = shift;

my ($cat, $url, $dir, @related, $relation, $page_num, $next_page, $numlinks);
local ($category, $links, $title_linked, $title, $total, $category_name, $category_name_escaped, $subcat_links);
local ($description, $related, $meta_name, $meta_keywords, $header, $footer, $next, $prev);

# Go through each category and build the appropriate page.
CATEGORY: foreach $cat (sort keys %category) {
next CATEGORY if ($cat =~ /^\s*$/); # How'd that get in here? =)
next CATEGORY if ($build_single and ($build_single ne $cat));

$url = "$build_root_url/" . &urlencode($cat) . "/";
$use_html ?
print qq|Building Category: <A HREF="$url" TARGET="_blank">$cat</A>\n| :
print qq|Building Category: $cat\n|;
print "\tSubcategories: " . ($#{$subcategories{$cat}} + 1) . "\n";
print "\tLinks: " . (($#{$links{$cat}}+1) / ($#db_cols+1)) . "\n";

# Let's make sure the directory exists, build it if it doesn't.
$dir = &build_dir ($cat);
print "\tDirectory: $dir\n";
print "\tFilename : $dir/$build_index\n";

# We set up all the variables people can use in &site_html.pl.
($description, $related, $meta_name, $meta_keywords, $header, $footer) = @{$category{$cat}}[2..7];

# Get the header and footer from file if it exists, otherwise assume it is html.
if ($header && (length($header) < 20) && ($header !~ /\s+/) && (-e "$db_header_path/$header")) {
open (HEAD, "<$db_header_path/$header") or &cgierr ("Unable to open header file: $db_header_path/$header. Reason: $!");
$header = "";
while (<HEAD>) {
$header .= $_;
}
close HEAD;
}
if ($footer && (length($footer) < 20) && ($footer !~ /\s+/) && (-e "$db_footer_path/$footer")) {
open (FOOT, "<$db_footer_path/$footer") or &cgierr ("Unable to open footer file: $db_footer_path/$footer. Reason: $!");
$footer = "";
while (<FOOT>) {
$footer .= $_;
}
close FOOT;
}
$title_linked = &build_linked_title ($cat);
$title = &build_unlinked_title ($cat);
$total = ($#{$links{$cat}} + 1) / ($#db_cols + 1);
$category_name = $cat;
$category_name_escaped = &urlencode ($cat);
$category_clean = &build_clean ($cat);

# Get list of links
$numlinks = ($#{$links{$cat}} + 1) / ($#db_cols + 1);
$next = $prev = $links = "";
for ($i = 0; $i < $numlinks; $i++) {
%tmp = &array_to_hash ($i, @{$links{$cat}});
$links .= &site_html_link (%tmp);
}

# Get sub-category links
if ($#{$subcategories{$cat}} >= 0) {
$subcat_links = &site_html_subcat_links (@{$subcategories{$cat}});
}
else {
$subcat_links = "";
}
$links .= $subcat_links;

# Create the main page.
open (CAT, ">$dir/$build_index") or &cgierr ("unable to open category page: $dir/$build_index. Reason: $!");
print CAT &site_html_category;
close CAT;

print "\n";
}
}


Then add a new sub called site_html_subcat_links

Code:
sub site_html_subcat_links {
# --------------------------------------------------------
my (@subcat) = @_;
my ($url, $numlinks, $mod, $subcat, $category_name, $description, $output, $i);
local ($links);

foreach $subcat (sort @subcat) {

$numlinks = ($#{$links{$subcat}} + 1) / ($#db_cols + 1);
# Start with the sub-category title, don't show if no links
if ($numlinks > 0 ) {
$output .= "<P><FONT SIZE=2><B>";
$output .= &build_unlinked_title ($subcat);
$output .= "</B></FONT><BR>";
}
# Loop though the sub-catgeory links
for ($i = 0; $i < $numlinks; $i++) {
%tmp = &array_to_hash ($i, @{$links{$subcat}});
$output .= &site_html_link (%tmp);
}
# Get sub-category links
if ($#{$subcategories{$subcat}} >= 0) {
$subcat_links = &site_html_subcat_links (@{$subcategories{$subcat}});
}
else {
$subcat_links = "";
}
$output .= $subcat_links;
}
return $output;
}


In build_category_pages I've removed the related categories and sub-categories sections as well as span pages as these are no longer relevent.

site_html_subcat_links will call itself for each subsequent sub-category. So far I've tested it 3 levels down (ie. Cat/Sub_Cat/Sub_Cat/). In theory it should continue to work regardless of the number of levels involved.

This will build an index page for every sub-category, which is a bit over-kill. There's not really any point in building Cat/Sub-cat/Sub-cat/index.html as it will never be seen. Perhaps add something to the start of build_category_pages to only build if we are starting from a top-level category.

I'd really like to get this into a 2 column layout. The total number of links should be know but I can't find an easy way of keeping track of how many links have been built so far.