Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Display custom categories

Quote Reply
Display custom categories
 
Hi,

Im looking into a way of setting up Links SQL to display categories on the home page depending on a custom value in the category table and breaking each category link into different columns based on the total number, eg.

Browse by Brand
Cat link 1 - Cat link 6
Cat link 2 - Cat link 7
Cat link 3 - etc.
Cat link 4
Cat link 5

Here's an copy of the sub im using (based on one of Laura's from the UK high street site):

Code:
sub {
# -------------------------------------------------------------------
my $tags = shift;
my $cat_db = $DB->table('Category');
$cat_db->select_options ('ORDER BY Full_Name');
my $sth = $cat_db->select ( { FatherID => 0, CategoryType => 'Brand'}, ['Full_Name','ID','Name'] );
my $output=qq~~;
my $i=0;
while (my ($root_cat,$ID,$heading) = $sth->fetchrow_array) {
$i++;
my $url1 = $cat_db->as_url($root_cat);
$output.= qq~<a href="$CFG->{build_root_url}/$url1/" class="internal">$heading</a><br />\n~;
$cat_db->select_options ('ORDER BY Full_Name');
}
return $output;
}

Currently this sub outputs all the links in one column:

Cat link 1
Cat link 2
Cat link 3
Cat link 4
Cat link 5
Cat link 6
Cat link 7
etc.

Can anyone suggest a way to calculate the total number of categories due to be displayed and divide them by 2 or 3 to split them into 2 or 3 columns evenly?



thanks in advance for your help,

Charlie



Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile

Quote Reply
Re: [Chas-a] Display custom categories In reply to
I think you could use

my $hits = $cat_db->hits;
my $breakpoint = int ($hits / 3) + (($hits % 3) ? 1 : 0);

after the select line to get the total number and the breakpoints.

Then you could use something like

($i > 0) and !($i % $breakpoint) and ($output .= qq~</td><td>~);

inside the while loop.

You'll probably need to play around with this a bit to get it looking exactly how you want.
Quote Reply
Re: [afinlr] Display custom categories In reply to
 
Thanks Laura.

That works great! though i was thinking as there's a couple of other modifications Ill be adding to the way the categories display and as there's a built in method for displaying them already using <%category%>.

Is there a way (built in or able to be added that you can think of) for passing conditionals to the category tag? ie.
<%category(CategoryType => 'Brand')%>

thanks again,

Charlie

Last edited by:

Chas-a: Jan 26, 2004, 4:14 PM
Quote Reply
Re: [Chas-a] Display custom categories In reply to
Unfortunately not. You would need to write a global for this - and I think it would end up being similar to the one you have.

If you want to be able to control the look of the category by using subcategory.html you could try using this in the while loop

my $subcat;
$subcat->{URL} = $CFG->{build_root_url} . "/" . $cat_db->as_url ($root_cat) . "/" . $CFG->{build_index};
$subcat->{Short_Name} = $heading =~ m,.*/([^/]+)$, ? $1 : $heading;
$output .= Links::user_page ('subcategory.html', $subcat);

Edit: Actually I think you'll need to take all the category fields using fetchrow_hashref instead of just picking out Full_Name, Name and ID. Then you would have

while (my $subcat = $sth->fetchrow_hashref){

and all the properties would then be sent to the subcategory template.

Last edited by:

afinlr: Jan 26, 2004, 4:22 PM
Quote Reply
Re: [afinlr] Display custom categories In reply to
 
Best to go with your custom global suggestion.

Got a couple of questions for you.

The following global's working well but could you point me in the right direction for pulling in the Number_of_Links colum info also is it possable to pass conditionals to globals ie. using <%test_global('Brand')%> which variable within the sub would Brand be passed to?

thank you very much with your help with this.

Code:
sub {
# -------------------------------------------------------------------
my $tags = shift;
my $cat_db = $DB->table('Category');
$cat_db->select_options ('ORDER BY Full_Name');
my $sth = $cat_db->select ( { FatherID => 0, CategoryType => 'Brand'}, ['Full_Name','ID','Name'] );
my $output=qq~<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr valign="top"><td width="33%">~;
my $hits = $cat_db->hits;
my $breakpoint = int ($hits / 3) + (($hits % 3) ? 1 : 0);
my $i=0;
while (my ($root_cat,$ID,$heading) = $sth->fetchrow_array) {
$i++;
my $url1 = $cat_db->as_url($root_cat);
$output.= qq~<p class="menu-home"><a href="$CFG->{build_root_url}/$url1/">$heading</a></p>~;
$cat_db->select_options ('ORDER BY Full_Name');
my $sth2 = $cat_db->select ( { FatherID => $ID}, ['Full_Name','ID','Name'] );
if (($i eq $breakpoint) or ($i eq int($breakpoint * 2))){ $output.=qq~</td><td width="33%">~;}
}
$output.="</tr></table>";
return $output;
}



Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile

Quote Reply
Re: [Chas-a] Display custom categories In reply to
What about this - untested. sub {
# -------------------------------------------------------------------
my $type = shift;
my $cat_db = $DB->table('Category');
$cat_db->select_options ('ORDER BY Full_Name');
my $sth = $cat_db->select ( { FatherID => 0, CategoryType => $type});
my $output=qq~<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr valign="top"><td width="33%">~;
my $hits = $cat_db->hits;
my $breakpoint = int ($hits / 3) + (($hits % 3) ? 1 : 0);
my $i=0;
while (my $subcat = $sth->fetchrow_hashref) {
$i++;
$subcat->{URL} = $CFG->{build_root_url} . "/" . $cat_db->as_url ($root_cat) . "/" . $CFG->{build_index};
$subcat->{Short_Name} = $heading =~ m,.*/([^/]+)$, ? $1 : $heading;
$output .= Links::user_page ('subcategory.html', $subcat);

if (($i eq $breakpoint) or ($i eq int($breakpoint * 2))){ $output.=qq~</td><td width="33%">~;}
}
$output.="</tr></table>";
return $output;
}
Quote Reply
Re: [afinlr] Display custom categories In reply to
 
Got it, cheers Laura.

Because i need to only display categories with 1 or more links i needed to loop thru the $sth->fetchrow_array twice, 1st counting each loop, this gives the $breakpoint then apply that to the second loop for any categories with one or more, phewww think im getting the hang of Perl.

here's the working sub, its a bit of a hack - feel free to post any improvements.

cat_sub('Brand')

Code:
sub {
# -------------------------------------------------------------------
my $tags = shift;
my $cat_db = $DB->table('Category');
$cat_db->select_options ('ORDER BY Full_Name');
my $sth = $cat_db->select ( { FatherID => 0, CategoryType => $tags}, ['Full_Name','ID','Name','Number_of_Links'] );
my $sth2 = $cat_db->select ( { FatherID => 0, CategoryType => $tags}, ['Number_of_Links'] );
my $e=0;
while (my ($total) = $sth2->fetchrow_array) {
if ($total >= 1) {
$e++;
}
}
my $breakpoint = sprintf("%.0f", int($e / 3));
my $output=qq~<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr valign="top"><td width="33%">~;
my $i=0;
while (my ($root_cat,$ID,$heading,$total) = $sth->fetchrow_array) {
if ($total >= 1) {
$i++;
my $url1 = $cat_db->as_url($root_cat);
$output.= qq~<p class="menu-home"><a href="$CFG->{build_root_url}/$url1/">$heading</a></p>~;
$cat_db->select_options ('ORDER BY Full_Name');
}
if (($i eq $breakpoint) or ($i eq int($breakpoint * 2))){ $output.=qq~</td><td width="33%">~;}
}
$output.="</tr></table>";
return $output;
}

b.t.w Can anyone think of a way to round up this:

my $breakpoint = int($e / 3);

eg. from 29 categories equals 9 each colum (with 11 at the end) where it would split the categories more evenly if it equaled 10 by somehow applying a ceil in Perl?



Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile

Last edited by:

Chas-a: Jan 28, 2004, 2:01 PM
Quote Reply
Re: [Chas-a] Display custom categories In reply to
I think you can use a condition to stop the need for running the select twice:

sub {
# -------------------------------------------------------------------
my $type = shift;
my $cat_db = $DB->table('Category');
$cat_db->select_options ('ORDER BY Full_Name');
my $cond = GT::SQL::Condition->new( 'FatherID', '=', '0', 'CategoryType', '=', $type, 'Number_of_Links', '>', '0');
my $sth = $cat_db->select ( $cond, ['Full_Name','ID','Name','Number_of_Links'] );
my $hits = $cat_db->hits;
my $breakpoint = int ($hits / 3) + (($hits % 3) ? 1 : 0);
my $output=qq~<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr valign="top"><td width="33%">~;
my $i=0;
while (my ($root_cat,$ID,$heading,$total) = $sth->fetchrow_array) {
$i++;
my $url1 = $cat_db->as_url($root_cat);
$output.= qq~<p class="menu-home"><a href="$CFG->{build_root_url}/$url1/">$heading</a></p>~;
$cat_db->select_options ('ORDER BY Full_Name');
if (!($i % $breakpoint)){$output.=qq~</td><td width="33%">~;}
}
$output.="</tr></table>";
return $output;
}

If the columns don't look right, try moving the $i++; to the end of the while loop or change the initial value for $i to 1.
Quote Reply
Re: [afinlr] Display custom categories In reply to
 
Thats the one,

cheers your a star!



Comedy Quotes - Glinks 3.3.0, PageBuilder, StaticURLtr, CAPTCHA, User_Edit_Profile

Quote Reply
Re: [Chas-a] Display custom categories In reply to
I understand that this global will divide categories into equal parts.

How can this global be incorporated with the Yahoosubcats global?

I would like to divide the sub categories into 3 columns.