Gossamer Forum
Home : Products : Links 2.0 : Customization :

A New Subcat Mod

Quote Reply
A New Subcat Mod
Was playing around with links a while back and put together another sub categories on main page mod. It works like the related links. In admin.cgi when adding/modifying categories you get a multi select box of categories and just select which ones you want to display as subcats underneath that category.

This one differs from the other sub categories on main page mod in that you choose which sub categories you want displayed underneath the main one. Thus giving you complete control over which sub cats you want displayed on your main page. For instance you could have 4 sub cats for a category but you could choose only to display 2 of them on your main page.

For those that are interested here's the url:

http://cgi-resource.co.uk/pages/subcat2.shtml



Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: A New Subcat Mod In reply to
I tried this mod, but I don't get any subcats on the homepage -
any ideas why? here's the bottom of site_html_templates.pl -

sub site_html_print_cat {
# --------------------------------------------------------
# This routine determines how the list of categories will look.
# We now use a table to split the category name up into two columns.
# For each category you can use the following variables:
#
# $url : The URL to go to that category
# $category_name : The category name with _ and / removed.
# $category_descriptions{$subcat}: The category description (if any).
# $numlinks : The number of links inside that category (and subcategories).
# $mod : The newest link inside of that category.
#

my (@subcat) = @_;
my ($url, $numlinks, $mod, $subcat, $category_name, $description, $output, $i);
my ($half) = int (($#subcat+2) / 2);

# Print Header.
$output = qq|<div class="margin"><table width="80%" border="0" cellspacing="0" cellpadding="0"><tr><td class="catlist" valign="top">\n|;

foreach $subcat (sort @subcat) {
($description) = @{$category{$subcat}}[2];
foreach $subcat (sort @subcat) {
($description) = @{$category{$subcat}}[2];

my ($subcats) = @{$category{$subcat}}[8];

@subcats = split(/\Q$db_delim\E/, $subcats); $subcats = "";
foreach $scat (@subcats) {
$subcats .= qq|<a href="$build_root_url/|;
$subcats .= &urlencode($scat);
$subcats .= qq|/$build_index">|;

if ($scat =~ m,.*/([^/]+)$,) { $subcats .= &build_clean($1);}
else {
$subcats .= &build_clean($scat);
}

$subcats .= qq|</a> |;
}


# First let's get the name, number of links, and last modified date...
$url = "$build_root_url/" . &urlencode($subcat) . "/";
if ($subcat =~ m,.*/([^/]+)$,) { $category_name = &build_clean($1); } else { $category_name = &build_clean($subcat); }
$numlinks = $stats{"$subcat"}[0];
$mod = $stats{"$subcat"}[1];

# We check to see if we are half way through, if so we stop this table cell
# and begin a new one (this lets us have category names in two columns).
if ($i == $half) {
$output .= qq|</td><td class="catlist" valign="top">\n|;
}
$i++;

# Then we print out the name linked, new if it's new, and popular if its popular.
$output .= qq|<dl><dt><strong><a class="link" href="$url">$category_name</a></strong> <small class="numlinks">($numlinks)</small> |;
$output .= qq|<small><sup class="new">new</sup></small>| if (&days_old($mod) < $db_new_cutoff);
$output .= qq|</dt>|;
$output .= qq|<dd><span class="descript">$description
$subcats</span></dd>| if (!($description =~ /^[\s\n]*$/));
$output .= qq|</dl>|;
}

# Don't forget to end the unordered list..
$output .= "</td></tr></table></div>\n";
return $output;
}
}
1;




Quote Reply
Re: A New Subcat Mod In reply to
Well for a start you have put:

foreach $subcat (sort @subcat) {
($description) = @{$category{$subcat}}[2];
foreach $subcat (sort @subcat) {
($description) = @{$category{$subcat}}[2];


I didn't say to do that in the instructions! Only use it once ie. delete those two lines you added.


Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: A New Subcat Mod In reply to
well spotted... thanks!
but I still get no subcats on the homepage - the only other file I have altered is the category.def -


Footer => [7, 'alpha', 40, 75, 0, '', ''],
Subcats => [8, 'alpha', 0, 255, 0, '', '']

);


and the db_utils.pl -

sub build_select_field {
# --------------------------------------------------------
# Builds a SELECT field based on information found
# in the database definition.
#
my ($column, $value, $name, $mult) = @_;
my ($size, %values);

$name || ($name = $column);
$size || ($size = 1);
$db_select_fields{'Mult-Subcats'} = join (",", &category_list);

if (! exists $db_select_fields{$column}) {
$db_select_fields{$db_cols[$db_category]} = $db_select_fields{'Mult-Related'} = join (",", &category_list);
}
if ($mult) {
@fields = split (/\,/, $db_select_fields{"Mult-$column"});
%values = map { $_ => 1 } split (/\Q$db_delim\E/, $value);
}
else {
@fields = split (/\,/, $db_select_fields{$column});
$values{$value}++;
}
($#fields >= 0) or return "error building select field: no select fields specified in config for field '$column'!";

$output = qq|<SELECT NAME="$name" $mult SIZE=$size><OPTION>---|;
foreach $field (@fields) {
$values{$field} ?
($output .= "<OPTION SELECTED>$field\n") :
($output .= "<OPTION>$field");
}
$output .= "</SELECT>";
return $output;

hope you can help....

Quote Reply
Re: A New Subcat Mod In reply to
Sorry not sure what your doing wrong, It's working fine for me! Have you refreshed your browser when viewing the home page? Also open categores.db and check you've defined some subcats there.

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: A New Subcat Mod In reply to
Does the mod work with the non-English mod?

Anyway, I have not tried your latest mod yet, BUT it will definitely be an asset to the LINKS community.

Keep up the good work,

Joe


Quote Reply
Re: A New Subcat Mod In reply to
Does the mod work with the non-English mod?

I don't know I've never seen or used the non english mod.

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: A New Subcat Mod In reply to
In Reply To:
It works like the related links
Sorry, but I don't recall that working for anybody except you Wink

Quote Reply
Re: A New Subcat Mod In reply to
Thanks for your reply. The mod I am refering too is the one in the resources >> modification section. Its corect name is Links 2 Non-english Modifications - Allows non english characters in the category name.

Joe

Quote Reply
Re: A New Subcat Mod In reply to
" open categores.db and check you've defined some subcats there."

I was starting from scratch with an empty categories.db file...
should changes be made to this file if you are starting from scratch?



Quote Reply
Re: A New Subcat Mod In reply to
'should changes be made to this file if you are starting from scratch? '

No what I mean is you need add that extra field in category.def but you also need to go into admin.cgi and choose the categories you want to display as subcats.

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: A New Subcat Mod In reply to
Hi Glennu,

first i want to say Thank you for writing this nice script. :)

But I have a problem. The Subcats are not shown on the main page.

http://www.revierloewenfans.de/links

I donīt find an error... checked your description three times

---

categories.db
2|REVIERLOEWEN|Alle Links haben die Eishockey-Nr. 1 im Pott als Thema.||||||
3|DEL|||||||
4|DEB|Alles aus dem Bereich des Deutschen Eishockey-Bundes.||||||
7|Augsburger_Panther|||||||DEL

ID 7 should be a subcat from DEL (ID 3)

---

category.def

# Database Definition: CATEGORIES
# --------------------------------------------------------
# Definition of your database file.
%db_def = (
ID => [0, 'numer', 5, 8, 1, '', ''],
Name => [1, 'alpha', 40, 75, 1, '', '^[\w\d/_-]+$'],
Description => [2, 'alpha', '40x3', 500, 0, '', ''],
Related => [3, 'alpha', 0, 255, 0, '', ''],
'Meta Description' => [4, 'alpha', 40, 75, 0, '', ''],
'Meta Keywords' => [5, 'alpha', 40, 75, 0, '', ''],
Header => [6, 'alpha', 40, 75, 0, '', ''],
Footer => [7, 'alpha', 40, 75, 0, '', ''],
Subcats => [8, 'alpha', 0, 255, 0, '', '']
);


---

site_html_templates

my (@subcat) = @_;
my ($url, $numlinks, $mod, $subcat, $category_name, $description, $output, $i);
my ($half) = int (($#subcat+2) / 2);

# Print Header.
$output = qq|<div class="margin"><table width="80%" border="0" cellspacing="0" cellpadding="0"><tr><td class="catlist" valign="top">\n|;

foreach $subcat (sort @subcat) {
($description) = @{$category{$subcat}}[2];
my ($subcats) = @{$category{$subcat}}[8];

@subcats = split(/\Q$db_delim\E/, $subcats); $subcats = "";
foreach $scat (@subcats) {
$subcats .= qq|<a href="$build_root_url/|;
$subcats .= &urlencode($scat);
$subcats .= qq|/$build_index">|;

if ($scat =~ m,.*/([^/]+)$,) { $subcats .= &build_clean($1);}
else {
$subcats .= &build_clean($scat);
}

$subcats .= qq|</a> |;
}


# First let's get the name, number of links, and last modified date...
$url = "$build_root_url/" . &urlencode($subcat) . "/";
if ($subcat =~ m,.*/([^/]+)$,) { $category_name = &build_clean($1); } else { $category_name = &build_clean($subcat); }
$numlinks = $stats{"$subcat"}[0];
$mod = $stats{"$subcat"}[1];

# We check to see if we are half way through, if so we stop this table cell
# and begin a new one (this lets us have category names in two columns).
if ($i == $half) {
$output .= qq|</td><td class="catlist" valign="top">\n|;
}
$i++;

# Then we print out the name linked, new if it's new, and popular if its popular.
$output .= qq|<dl><dt><strong><a class="link" href="$url">$category_name</a></strong> <small class="numlinks">($numlinks)</small> |;
$output .= qq|<small><sup class="new">new</sup></small>| if (&days_old($mod) < $db_new_cutoff);
$output .= qq|</dt>|;
$output .= qq|<dd><span class="descript">$description
$subcats</span></dd>| if (!($description =~ /^[\s\n]*$/));
$output .= qq|</dl>|;
}

# Don't forget to end the unordered list..
$output .= "</td></tr></table></div>\n";
return $output;
}

1;

---

db_utils.pl

$db_select_fields{'Mult-Subcats'} = join (",", &category_list);
if (! exists $db_select_fields{$column}) {
$db_select_fields{$db_cols[$db_category]} = $db_select_fields{'Mult-Related'} = join (",", &category_list);
}

---

Do you find an error? Many Thanks in advance for your time!

joergh

Quote Reply
Re: A New Subcat Mod In reply to
It works great for me. I have seen one thing that wouldn't cause me a problem but would cause a problem with it on your site. I have descriptions for all my categories where as you don't. So change the following:

Replace:

$output .= qq|<dd><span class="descript">$description
$subcats</span></dd>| if (!($description =~ /^[\s\n]*$/));

With:

$output .= qq|<dd><span class="descript">$description
</span></dd>| if (!($description =~ /^[\s\n]*$/));
$output .= qq|<dd><span class="descript">$subcats</span></dd>|;


This will fix your problem.


Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: A New Subcat Mod In reply to
Great, it works. I just added a description to every category.

Thank you very much!

There is just one question left (i installed links 2.0 today, maybe i donīt have the overview about the structure of links 2 yet). How can i create "real" sub cats? The aim is to see a subcat on my page a) as a link on the homepage (like now after your hint :) ) and b) if i click the cat "DEL" subcats at the top then the normal links of this category. Do you understand what i mean? :) "Augsburger Patner" should not be a main category.

http://www.revierloewenfans.de/links

best wishes

joergh

Quote Reply
Re: A New Subcat Mod In reply to
In Reply To:
How can i create "real" sub cats?
Open your admin.cgi and read the HELP section OR read the FAQ in the Links -> FAQ section of the Resources section, PLEASE!

Regards,

Eliot Lee
Quote Reply
Re: A New Subcat Mod In reply to
Thank you,

i scrolled through the faqs but didnīt see the topic i needed one hour ago. Now i scrolled again and here we go now :)

Best wishes

joergh

Quote Reply
Re: A New Subcat Mod In reply to
Browsing/Reading is the key...not scrolling.

Guess you missed it!

Links -> FAQs
How do you create subcategories?
http://www.gossamer-threads.com/...es/Detailed/869.html

And if you searched for subcategories in the Resources, you would've found it as well....

http://www.gossamer-threads.com/...?query=subcategories

Wow! Look at the first search result! Tongue

And, remember, what I said about OPENING your admin.cgi script and reading the HELP files. Guess you missed the following text, too....

In Reply To:

Common Questions

These are questions I get asked all the time, so please read!

How do I create subcategories?

Easy, just name your category appropriately. For instance, if you have a category named 'Computers', to have a subcategory named Hardware just click on Add Category, and name it 'Computers/Hardware'.
You can have as many levels as you like 'Computers/Hardware/Video Cards/Diamond' for instance, but just make sure you have the appropriate categories before hand (i.e. in the above example, you would need to have 'Computers', 'Computers/Hardware' and 'Computers/Hardware/Video Cards' first.

Why do no links show up in the Top 10 listings?

A link must have at least 10 votes before it will be placed in the top rated report.
Please be sure to check out the Frequently Asked Questions list for other common problems. If you are still stuck or looking for more tips and tricks, stop by the Support Forum!


Regards,

Eliot Lee
Quote Reply
Re: A New Subcat Mod In reply to
Hi glennu,

I installed this mod today and it works great! I have two questions please:

1] I would like to list the subcats before the description. What do I change?

2] I would like to put commas inbetween each subcat and not have one after the last subcat listed.

Would you tell me how to do this? I'm sure it is easy but I don't know much about perl. I figured out how to put extra spaces inbetween the subcats listed, but if I add a comma there, the comma would also print after the last subcat listed, correct?

Thank you very much for your help,
Cheryle

Quote Reply
Re: A New Subcat Mod In reply to
1. Where I've said add:

$output .= qq|<dd><span class="descript">$description</span></dd>| if (!($description =~ /^[\s\n]*$/));
$output .= qq|<dd><span class="descript">$subcats</span></dd>|;


Instead use:

$output .= qq|<dd><span class="descript">$subcats</span></dd>|;
$output .= qq|<dd><span class="descript">$description</span></dd>| if (!($description =~ /^[\s\n]*$/));


2. Instead of:

foreach $subcat (sort @subcat) {
($description) = @{$category{$subcat}}[2];

my ($subcats) = @{$category{$subcat}}[8];

@subcats = split(/\Q$db_delim\E/, $subcats); $subcats = "";
foreach $scat (@subcats) {
$subcats .= qq|<a href="$build_root_url/|;
$subcats .= &urlencode($scat);
$subcats .= qq|/$build_index">|;

if ($scat =~ m,.*/([^/]+)$,) { $subcats .= &build_clean($1);}
else {
$subcats .= &build_clean($scat);
}

$subcats .= qq|</a> |;
}


Use:


foreach $subcat (sort @subcat) {
($description) = @{$category{$subcat}}[2];

my ($subcats) = @{$category{$subcat}}[8];
my $counter= 0;
@subcats = split(/\Q$db_delim\E/, $subcats); $subcats = "";
foreach $scat (@subcats) {

if ($counter != 0){
$subcats .= qq|,|;
}
$subcats .= qq|<a href="$build_root_url/|;
$subcats .= &urlencode($scat);
$subcats .= qq|/$build_index">|;

if ($scat =~ m,.*/([^/]+)$,) { $subcats .= &build_clean($1);}
else {
$subcats .= &build_clean($scat);
}
$subcats .= qq|</a> |;


$counter++;
}


Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: A New Subcat Mod In reply to
Hi glennu,

Thank you so much! Works perfect! Bet you knew that **grin**. I made one change as follows:

YOUR CODE
In Reply To:
Use:

foreach $subcat (sort @subcat) {
($description) = @{$category{$subcat}}[2];

my ($subcats) = @{$category{$subcat}}[8];
my $counter= 0;
@subcats = split(/\Q$db_delim\E/, $subcats); $subcats = "";
foreach $scat (@subcats) {

if ($counter != 0){
$subcats .= qq|,|;
}
$subcats .= qq|<a href="$build_root_url/|;
$subcats .= &urlencode($scat);
$subcats .= qq|/$build_index">|;

if ($scat =~ m,.*/([^/]+)$,) { $subcats .= &build_clean($1);}
else {
$subcats .= &build_clean($scat);
}
$subcats .= qq|</a> |;


$counter++;
}
CHANGED TO:
In Reply To:
Use:

foreach $subcat (sort @subcat) {
($description) = @{$category{$subcat}}[2];

my ($subcats) = @{$category{$subcat}}[8];
my $counter= 0;
@subcats = split(/\Q$db_delim\E/, $subcats); $subcats = "";
foreach $scat (@subcats) {

if ($counter != 0){
$subcats .= qq|, |;
}
$subcats .= qq|<a href="$build_root_url/|;
$subcats .= &urlencode($scat);
$subcats .= qq|/$build_index">|;

if ($scat =~ m,.*/([^/]+)$,) { $subcats .= &build_clean($1);}
else {
$subcats .= &build_clean($scat);
}
$subcats .= qq|</a>|;


$counter++;
}
This allows the display to be:
subcat, subcat, subcat
not
subcat ,subcat ,subcat Smile

I've been reading all the posts you answered this a.m. and you have been busy. Thanks for taking the time to answer my request. It is greatly appreciated!

Cheryle

Quote Reply
Re: A New Subcat Mod In reply to
No problem.

Yeah good point, I forgot about changing that bit!

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: A New Subcat Mod In reply to
I have tryed a few but this on is (exept the layout center) perfect Instaled it in one time. Great desription on how to ...

regards alfred

Quote Reply
Re: [glennu] A New Subcat Mod In reply to
this mod does not work with non-english mod. does it ? it seems a huge problem as i can see in the GT forum.

do you have any advice about that ?

i don t get any answer that works in the forum to mix correctly 'subcat' and 'non english' mods.



tanx



Smile
Quote Reply
Re: [glennu] A New Subcat Mod In reply to
this is a fantastic mod... one thing i would request though - is there a way to have the subcats already populated until you remove them?

for instance if i have web as the main category and then i add web/design web/webmastering web/hosting can it by default display

web
design, webmastering, hosting

until i remove one of the subcats from the admin. ie if i removed webmastering and only if i removed it it would say

web
design, hosting

?




Vote Stinky
Quote Reply
Re: [security_man] A New Subcat Mod In reply to
Try this very simple "Yahoo-style" mod. There are further options available to select which subcats get listed, and in what order, if you need to, but require more changes...

http://www.gossamer-threads.com/perl/gforum/gforum.cgi?post=234269#234269


Leonard
aka PerlFlunkie