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
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
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

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