Gossamer Forum
Quote Reply
Previous/Next Categories
How to create Previous/Next subcategory links in any given Category?

For example under Europe Category if the visitor is in "France" subcategory page the previous link should go to Finland and next link to GERMANY.

The sequence should be alphabetical and it should loop.(see examples 3 and 4)



1.
<FINLAND GERMANY>
FRANCE


2.
<FRANCE GREECE>
GERMANY


3.
<VATICAN ANDORRA>
ALBANIA


4.
<UKRAINE ALBANIA>
VATICAN


Thanks.
Quote Reply
Re: [AMIXIMA] Previous/Next Categories In reply to
You do like asking the harder questions Wink

Totally untested - but give this a go:

get_next_and_prev_cats
Code:
sub {
my ($this_id,$fatherid) = @_;

my $tbl = $DB->table("Category");
$tbl->select_options("ORDER BY Name ASC");
my $sth = $tbl->select( { FatherID => $fatherid } ) || die $GT::SQL::error;
my @loop;
while (my $hit = $sth->fetchrow_hashref) {
push @loop, $hit;
}

my ($prev,$next) = ({},{}); # set hashrefs, or else we may get errors!
for (my $i =0; $i <= $#loop; $i++) {
if ($loop[$i]->{ID} == $this_id) {
$prev = $loop[$i-1];
$next = $loop[$i+1];

if ($prev->{ID}) {
$prev->{URL} = $CFG->{build_root_url} . "/" . $DB->table("Category")->as_url( $prev->{Full_Name} ) . "/";
}
if ($next->{ID}) {
$next->{URL} = $CFG->{build_root_url} . "/" . $DB->table("Category")->as_url( $next->{Full_Name} ) . "/";
}
}
}

my $parent_cat = $DB->table('Category')->get( { ID => $fatherid } );
$parent_cat->{URL} = $CFG->{build_root_url} . "/" . $DB->table("Category")->as_url( $parent_cat->{Full_Name} ) . "/";

return {
'next_cat' => $next,
'prev_cat' => $prev,
'parent_cat' => $parent_cat
}

}

Call with:

Code:
<%get_next_and_prev_cats($ID,$FatherID)%>

<%if next_cat.ID%>
<a href="<%next_cat.URL%>"><%next_cat.Name%></a>
<%endif%>

<%if parent_cat.ID%>
<a href="<%parent_cat.URL%>"><%parent_cat.Name%></a>
<%endif%>

<%if prev_cat.ID%>
<a href="<%prev_cat.URL%>"><%prev_cat.Name%></a>
<%endif%>

Totally untested, but should work

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Mar 9, 2012, 9:14 AM
Quote Reply
Re: [Andy] Previous/Next Categories In reply to
Hi Andy,

Thank you so much for your answer! I'll test the code and let you know if everything works fine.

Thanks again,

AMIXIMA
Quote Reply
Re: [AMIXIMA] Previous/Next Categories In reply to
I'm getting following error while adding the code:

Unable to compile 'get_next_and_prev_cats': Global symbol "$sth" requires explicit package name at (eval 34) line 6.

Please help.

Thanks.
Quote Reply
Re: [Andy] Previous/Next Categories In reply to
Also I noticed that the previous/next function is working by ID values not 'Name' values. How can i change that also?
Quote Reply
Re: [AMIXIMA] Previous/Next Categories In reply to
Hi,

There was a typo in the above global. Try the updated version

Quote:
Also I noticed that the previous/next function is working by ID values not 'Name' values. How can i change that also?

It works by the FatherID (the ID of the "parent" category). Thats the only way you will be able to do what you want Wink

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Previous/Next Categories In reply to
Hi Andy

Thank you, the code is working now!

1. I understand that it must work by the FatherID but the loop is done using ID values . Some of my categories don't have incremental ID values and what i would like to do is to sort the table by Name before looping.

Select * Category where FatherID=$FatherID ORDER BY NAME


2. How to get also father Category link so now it will be like?


<Previous Category ^Top Category NEXT Category>


Let me know if it is doable.
















Quote Reply
Re: [AMIXIMA] Previous/Next Categories In reply to
Try changing:

Code:
my $sth = $DB->table("Category")->select( { FatherID => $fatherid } ) || die $GT::SQL::error;

to

Code:
my $tbl = $DB->table("Category");
$tbl->select_options("ORDER BY Name ASC");
my $sth = $tbl->select( { FatherID => $fatherid } ) || die $GT::SQL::error;

Quote:
2. How to get also father Category link so now it will be like?
<Previous Category ^Top Category NEXT Category>
Let me know if it is doable.

You mean if no "next" or "previous" exists, it should get the next category "above" it? Not sure thats easily achievable (afraid I can't really dedicate any more time to this, as I'm up to my neck in work, and already spent a fair bit of time working out how to do the current global for ya Wink)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Previous/Next Categories In reply to
Now the category sequence works properly!

about the top Category link, all I wanted is a link to Parent Category

Previous Category( done)
Parent Category
Next Category (done)

For example all Countries under Europe will have a link to Europe page.


Thanks.
Quote Reply
Re: [AMIXIMA] Previous/Next Categories In reply to
Hi,

I modified the global above a bit (including the other tweak we did for ORDER)... you should now have access to <%parent_cat.xxx%> fields as well

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Previous/Next Categories In reply to
Everything works now!

Thank you for your time.