Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Header Image for Main Categories

Quote Reply
Header Image for Main Categories
Hi

I was wondering whether anyone knew how to get an image that is displayed for a category to be also displayed for all subcategories.

I created a field in the category .def called Category_Image

Then I use this to display the image :
<%if Category_Image%>
<img src="<%build_images_url%>/<%Category_Image%>" border=0 />
<%endif%>

which doesn't provide the appropriate path but i just uploaded the same file to where it's looking for it (Can't be bothered to figure out why paths for uploaded files get some weird and wonderful path when called through LinksSQL something to do with the fact that its hashed I think and it works like this anyway)

However I would like to get the category image to display for category "1" for example and all categories that are subcategories of "1" but at the moment it will only display for category "1" which is understandable from the code.

Does anybody know how I can get this to work ?

Thanks, John

Edit :

I have got it to work using this :
<%if Category_Image%>
<img src="<%build_images_url%>/<%Category_Image%>" border=0 />
<%elsif FatherID eq '1'%>
<img src="<%build_images_url%>/categoryheader_1.jpg" border=0 />
<%elsif FatherID eq '2'%>
<img src="<%build_images_url%>/categoryheader_2.jpg" border=0 />
<%endif%>

I only have 9 main categories but if anyone has a cleaner way of doing it ???

Thanks
Significant Media

Last edited by:

Jag: Nov 19, 2004, 7:53 PM
Quote Reply
Re: [Jag] Header Image for Main Categories In reply to
Assuming that that is really what you have called your images, what about something like this:

<%if Category_Image%>
<img src="<%build_images_url%>/<%Category_Image%>" border=0 />
<%elsif FatherID > 0%>
<img src="<%build_images_url%>/categoryheader_<%FatherID%>.jpg" border=0 />
<%endif%>
Quote Reply
Re: [afinlr] Header Image for Main Categories In reply to
Hi Laura,

Very clever that's true. Basically at the top of the categories page whiwh is like a magazine page for me I want to have a nice graphic header that reminds the person which of the nine main categories they are in.

Thus if I create a subcategory in any of these nine categories there will be a header from one of the main categories but I suppose that will stop working if I create a subcategory in a subcategory since the top nine categories will no longer be the fatherID ?? Or is Father ID equal to the first below root ?

I suppose I could put :
<%if Category_Image%>
<img src="<%build_images_url%>/<%Category_Image%>" border=0 />
<%elsif FatherID > 0 < 9%>
<img src="<%build_images_url%>/categoryheader_<%FatherID%>.jpg" border=0 />
<%endif%>

So that it doesn't create errors if I create a sub in a sub ?

Anyway Thanks very much Laura and have a nice week-end !

John
Significant Media
Quote Reply
Re: [Jag] Header Image for Main Categories In reply to
Yes - I think you may need
<%elsif FatherID > 0 and FatherID < 9%>

However, this will mean that you don't get the image in the second level categories.
I think you could do this with quite a simple global if you want the image in every subcategory?
Quote Reply
Re: [afinlr] Header Image for Main Categories In reply to
Oh you mean like a global that would look for the top father ID unless the FatherID to be displayed is equal to O ?

Oh and thanks for correcting me on the above Smile

John
Significant Media
Quote Reply
Re: [Jag] Header Image for Main Categories In reply to
Hi, this is untested so apologies in advance if it doesn't work.

Try this global called <%get_cat_image%>

sub {
my $tags = shift;
if ($tags->{Category_Image}){return $tags->{Category_Image};}
else {
my $cat = $tags->{Full_Name};
$cat =~ s,^([^/]).*$,$1,;
my $cat_db = $DB->table('Category');
my $image = $cat_db->select ( { FatherID => 0, Full_Name => $name}, ['Category_Image'] )->fetchrow_array;
return $image;
}
}
Quote Reply
Re: [afinlr] Header Image for Main Categories In reply to
Hi Laura,

Thaks a lot for this I'll try it out tomorrow night. Just got back and it's nearly 6 o'clock here but just to thank you for your time and effort.

BTW Your extra stats plug-in is great I've just set it up on my new server and it works like a charm and installed straight away!

Have a nice week-end

John
Significant Media
Quote Reply
Re: [Jag] Header Image for Main Categories In reply to
Thanks. Just noticed a problem with the global above:

$cat =~ s,^([^/]+)/.*$,$1,;
Quote Reply
Re: [afinlr] Header Image for Main Categories In reply to
Hi Laura,

Did the mod sugegsted :

sub {
my $tags = shift;
if ($tags->{Category_Image}){return $tags->{Category_Image};}
else {
my $cat = $tags->{Full_Name};
$cat =~ s,^([^/]+)/.*$,$1,;
my $cat_db = $DB->table('Category');
my $image = $cat_db->select ( { FatherID => 0, Full_Name => $name}, ['Category_Image'] )->fetchrow_array;
return $image;
}
}

with
<%get_cat_image> inserted in category.html

I'm getting this error mesage :
Unable to compile 'get_cat_image': Global symbol "$name" requires explicit package name at (eval 24) line 8.
Significant Media
Quote Reply
Re: [Jag] Header Image for Main Categories In reply to
Hi,

Sorry, no idea where $name came from.

my $image = $cat_db->select ( { FatherID => 0, Full_Name => $cat}, ['Category_Image'] )->fetchrow_array;
Quote Reply
Re: [afinlr] Header Image for Main Categories In reply to
Hi Laura,

Just tried it out and it works. Thanks again for some sound code.

I had also added my simple code to the detailed.html page since it's a continuation of the magazine on my site. While the simple code lines bring up the header so far I can't get the above global to produce the filename like it does in category.html (I've only got the nine top categories and only two subs in two of them so far so it's a really simple structure).

However I had used Andy's getfatherID global on the detailed.html http://www.gossamer-threads.com/...i?post=251958#251958 with my code so that I can get the fatherid of the link's category and got it to work that way.

I left this global and added yours below but it didn't seem to pick up from it I guess it's difficult to see what's happening when there's no output.

Anyway at the moment I'll just keep it simple from a category point of view.

Thanks again for you help.

John
Significant Media
Quote Reply
Re: [Jag] Header Image for Main Categories In reply to
Hi,

Try this modification to Andy's global:

sub {

my $ID = $_[0];
my $table = $DB->table('CatLinks');
my $sth = $table->select( { LinkID => $ID } ) || return "Error: $GT::SQL::error";

my $CatID;
while (my $hit = $sth->fetchrow_hashref) {
$CatID = $hit->{CategoryID};
}

my $table = $DB->table('Category');
my $sth = $table->select( { ID => $CatID } ) || return "Error: $GT::SQL::error";

my ($FatherID,$Full_Name,$CatIm);
while (my $hit = $sth->fetchrow_hashref) {
$FatherID = $hit->{FatherID};

$Full_Name = $hit->{Full_Name};
$CatIm = $hit->{Category_Image};
}


return { FatherID => $FatherID, Full_Name => $Full_Name, $CatIm => $CatIm }

}
Quote Reply
Re: [afinlr] Header Image for Main Categories In reply to
Thanks Laura,

I'll try it out tomorrow. The fuse actually exploded on the surge box I've got my work computer plugged into so I want to get a new one before I plug everything back in to get to my templates etc. I'm using the main computer to type this that's on a different electricity circuit.

Thanks again

John
Significant Media
Quote Reply
Re: [afinlr] Header Image for Main Categories In reply to
Ok so many thanks to Laura for this works fine.

Global provided by Laura :
I called it "get_cat_image" (as Laura suggests)

sub {
my $tags = shift;
if ($tags->{Category_Image}){return $tags->{Category_Image};}
else {
my $cat = $tags->{Full_Name};
$cat =~ s,^([^/]+)/.*$,$1,;
my $cat_db = $DB->table('Category');
my $image = $cat_db->select ( { FatherID => 0, Full_Name => $cat}, ['Category_Image'] )->fetchrow_array;
return $image;
}
}


On the category template I use this at the top of the page to display a category header :
<%if get_cat_image%>
<img src="<%build_images_url%>/header_image/<%get_cat_image%>" border=0 />
<%endif%>



For the detailed page I add this global by Andy and specifically modified by Laura :
I called it "detail_father_id"

sub {

my $ID = $_[0];
my $table = $DB->table('CatLinks');
my $sth = $table->select( { LinkID => $ID } ) || return "Error: $GT::SQL::error";

my $CatID;
while (my $hit = $sth->fetchrow_hashref) {
$CatID = $hit->{CategoryID};
}

my $table = $DB->table('Category');
my $sth = $table->select( { ID => $CatID } ) || return "Error: $GT::SQL::error";

my ($FatherID,$Full_Name,$CatIm);
while (my $hit = $sth->fetchrow_hashref) {
$FatherID = $hit->{FatherID};
$Full_Name = $hit->{Full_Name};
$CatIm = $hit->{Category_Image};
}


return { FatherID => $FatherID, Full_Name => $Full_Name, $CatIm => $CatIm }

}


Then in the detailed.html page I insert at the top of the page to display a category header :
<%detail_father_id($ID)%>
<%if get_cat_image%>
<img src="<%build_images_url%>/header_image/<%get_cat_image%>" border=0 />
<%endif%>

Obviously all this depends on having created an image field in the category table that in this example is called "Category_Image"
So far I haven't figured out how to get the hashed referenced path to the file so I just put via FTP in a folder called "header_image" thus the code :
<img src="<%build_images_url%>/header_image/<%get_cat_image%>" border=0 />

This may not be as clear as I think it is so please ask if it's not clear !

John

PS : just so that you know <%get_cat_image%> produces the name of the image file (without the path) and <%detail_father_id($ID)%> produces no visible output (it gets the fatherID that isn't otherwise called on the detailed.html page) in case you're expecting something else !
Significant Media
Quote Reply
Re: [afinlr] Header Image for Main Categories In reply to
Laura,

I was wondering if you know how I can can also get just the name of the parent category. When I'm on the category.html page and do a <%GT::Template::dump%> I can get the name of the parent category by using the following :
<%Dir_Name%>
<%Full_Name%>
<%Name%>
<%category_first%>
<%category_name%>
<%category_name_escaped%>
<%category_short%>
<%g%>

all on the page where the parent category is the category of the page.

However when going further down into the categories (hence the parent category is not the category of the page) none of the above display the parent category alone in the tags all tags display the present category preceded by the parent category or the last part of the category crumb

For example if a parent category is called "Sports" then two categories down I would have :
Sports > Indoor > Football
On this page however I would like to display the word "Sports" but none of the tags contain the word "Sports" alone.

I was thinking that by using the FatherID I could get the Full_Name of the parent category but when I insert the above tags in during the <%if get_cat_image%> <%endif%> I get the same results as on the category.html page. Do I need to call it in the sub and give it another name that I can call afterwards ? I tried using <%$cat%> but the tag is unknown.

Any ideas where I should be looking ?

Thanks, John
Significant Media
Quote Reply
Re: [Jag] Header Image for Main Categories In reply to
I think I understand. You'll need to return the name tag - something like this:

sub {
my $tags = shift;
if ($tags->{Category_Image}){return {Top_Name=>$tags->{Name}};}
else {
my $cat = $tags->{Full_Name};
$cat =~ s,^([^/]+)/.*$,$1,;
my $cat_db = $DB->table('Category');
my $image = $cat_db->select ( { FatherID => 0, Full_Name => $cat}, ['Category_Image'] )->fetchrow_array;
return {Category_Image=>$image, Top_Name=>$cat}
}
}

Then you'll need to use
<%get_cat_image%>
<img src="<%build_images_url%>/header_image/<%Category_Image%>" border=0 />
<%Top_Name%>

Last edited by:

afinlr: Jul 31, 2005, 1:02 PM
Quote Reply
Re: [afinlr] Header Image for Main Categories In reply to
Thanks once again Laura that works fine I can get the image like before and it also allows me to display the name as well. Perfect !

Have a nice day Smile

Thanks, John
Significant Media