Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Different links_loop for different link types

Quote Reply
Different links_loop for different link types
Hi Guys,

I am setting up a directory where I have 3 different types of listings: Premium, Featured and Standard. I have a select field in the links table called Link_Type with the 3 options, and each of the 3 types is displayed differently but grouped together on the category pages .
So the basic category template contains:

Premium Listings:
<%loop links_loop%>
<%if Link_Type eq 'Premium'%>
<%include link.html%>
<%endif%>
<%endloop%>

Feature Listings:
<%loop links_loop%>
<%if Link_Type eq 'Feature'%>
<%include link.html%>
<%endif%>
<%endloop%>

Standard Listings:
<%loop links_loop%>
<%if Link_Type eq 'Standard'%>
<%include link.html%>
<%endif%>
<%endloop%>

And my link.html template contains:

<%if Link_Type eq 'Premium'%>
<table>
html to display a Premium Listing
</table>
<%endif%>

<%if Link_Type eq 'Feature'%>
<table>
html to display a Feature Listing
</table>
<%endif%>

<%if Link_Type eq 'Standard'%>
<table>
html to display a Standard Listing
</table>
<%endif%>

So far so good, and this works fine.

However, I wish to use alternating table background colors for the links and this is where I am having problems Unsure

Adding :
bgcolor="<%if even%>#FFFFFF<%else%>#FCFBF5<%endif%>
to the table does output different background colors, but not quite as I had hoped. When the category page is viewed the Premium Listings are grouped together, as are the Feature and Standard, like I wanted, and some have alternating colours, but unfortunately not all.

The reason for this, I assume, is that links_loop returns a loop of all the links in that category and for example the Premium links might be the 1st, 2nd, 4th and 5th link returned in the loop (the 3rd could be a standard type link). So the background color will be set as odd, even, even and odd and so not alternate correctly.

The only way I see to get around this is to use 3 different links_loops in the page, a loop of just Premium type links, a loop of just Feature type links and a loop of just Standard type links, as opposed to a loop of all the links on the page.

Is a global the best way to approach this?

I have tried creating the following global called Premium_Link

sub {
my $db = $DB->table('Links');
my (@output, $sth);
my $sth = $db->select ( {Link_Type => 'Premium' });
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
return { Premium_Link_Loop => \@output }
}

and then in the category.html template using:

<%Premium_Link%>
<%loop Premium_Link_Loop%>
<%include link.html%>
<%endloop%>

This does solve the problem to a degree and the background colors do alternate correctly for the Premium Listings, however this of course calls in all the Premium type links and not just the ones in that category.

My knowledge of Globals and Perl is pretty limited and to be honest am not sure how to get around this problem, there may in fact be a much better way to approach it.

If anyone can give me some ideas, pointers or help in writing a global, it would be greatly appreciated.

Thanks
Mark
Quote Reply
Re: [marker] Different links_loop for different link types In reply to
You could try something like;

Code:
sub {

my $db = $DB->table('CatLinks','Links');
my (@output, $sth);
my $count = 0;
my $sth = $db->select ( { CategoryID => $_[0]}, { Link_Type => 'Premium' } );
while (my $hash = $sth->fetchrow_hashref) {
if ($count == 1) { $count = 0; $hash->{odd} = 1; }
if ($count == 0 && !$hash->{odd}) { $count++; $hash->{even} = 1; }
push @output, $hash;
}

return { Premium_Link_Loop => \@output }

}

...called with;

Code:
<%global_name%>
<%loop Premium_Link_Loop%>
<%include link.html%>
<%endloop%>

...and then in link.html, you can use;

<%if odd%>foo<%else%><%endif%>

Hope that helps :)

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: Apr 26, 2004, 6:44 AM
Quote Reply
Re: [Andy] Different links_loop for different link types In reply to
Hi Andy,

Unfortunately that didnt solve it.

Your code seems to make every Premium link display the odd bgcolor rather than alternate and as with my earlier code, it actually calls in every Premium type link in the database instead of just the ones in the category being displayed Unsure

Mark

Last edited by:

marker: Apr 26, 2004, 4:31 AM
Quote Reply
Re: [marker] Different links_loop for different link types In reply to
How about the modified version?

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] Different links_loop for different link types In reply to
Andy

That didn't do it Unsure
No Premium links at all are displayed in the categories now.

Mark
Quote Reply
Re: [marker] Different links_loop for different link types In reply to
Sorry, it should have been;

<%global_name($ID)%>


.. where $ID is the category ID (this should work on category pages).

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] Different links_loop for different link types In reply to
Ok Andy,
Things are looking up Smile

It now displays the correct Premium links in the category, however it is still setting the bgcolor as the "odd" color for all of the Premium Links

Mark
Quote Reply
Re: [marker] Different links_loop for different link types In reply to
How about the modified version? I *think* I've worked out where the problem is....

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] Different links_loop for different link types In reply to
Andy,

It still displays all the Premium Links in the same bgcolor.

As the initial global I tried worked for the bgcolors, I just tried removing a couple of lines from your latest one.
so it looks like:

Code:

sub {
my $db = $DB->table('CatLinks','Links');
my (@output, $sth);
my $sth = $db->select ( { CategoryID => $_[0]}, { Link_Type => 'Premium' } );
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
return { Premium_Link_Loop => \@output }
}


And it seems to be working... much to my surprise.

Is this just a fluke and if I leave it like this can I expect problems with it?

Mark
Quote Reply
Re: [marker] Different links_loop for different link types In reply to
Quote:
Is this just a fluke and if I leave it like this can I expect problems with it?

It may be fluke, or it may be the way that GT::SQL handles returned rows (i.e automatically assigning 'odd' and 'even' to them). If it works, and its ok in other categories, then I would just leave it as it is :)

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] Different links_loop for different link types In reply to
Andy,

So far it seems to be ok, so I will leave it and see if I get any issues with it over the next few days.

Thanks for your help mate, I really appreciate your time Smile

Cheers
Mark