Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Random Links Specified Per Category (continued ... )

Quote Reply
Random Links Specified Per Category (continued ... )
I am starting a new thread, because I still need some help with this, and I hope a new thread will help keep things clearer. (The other thread just confused me). Original Thread:
http://www.gossamer-threads.com/...orum_view_collapsed;

So, here goes:
I'd like to have 3 or so "random" links in each category be displayed. I will have seperate template sets for each category. Alex gave me some code to use that was supposed to limit the categories that the call pulls from, but I haven't gotten it to be successful.

I'd also like to be able to have "links" rotating on the home page, from specified categories.

1) a way to call (from seperate category pages) 3 links from that particular category and display them on the category page.

2) a way to call from the home page (or any other page) 3 random links from defined categories.

Random_Link global Alex gave me to try:

Code:
sub {
my $tags = shift;
my $link_db = $DB->table('Links','CatLinks');
my $cat_id = $tags->{Random_CatID};
my $limit = $tags->{Random_Limit} || 3;
my (@output, $sth);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
if ($cat_id) {
$sth = $link_db->select ( { CategoryID => $cat_id });
}
else {
$sth = $link_db->select;
}
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
return { Random_Loop => \@output }
}

and then to call the random link, place this code on the category page for that particluar category.:

Code:
<%set Random_Category = 9%>
<%Random_Link%>
<%loop Random_Loop%>
<p><%body_font%><A href="<%db_cgi_url%>/detail_page.cgi?ID=<%ID%>"><b><%Title%></b><br></A> <%Description%>
[<A href="<%db_cgi_url%>/detail_page.cgi?ID=<%ID%>">more</A>]</%body_font%>
<br>
<%endloop%>

The problem is, that it still pulls from other categories.

Pweeze help me define this Crazy

Last edited by:

Evoir: Dec 26, 2001, 2:13 PM
Quote Reply
Re: [Evoir] Random Links Specified Per Category (continued ... ) In reply to
What you could do is alter it to:

Code:

sub {
my $tags = shift;
my $link_db = $DB->table('Links','CatLinks');
my $cat_id = Your Category ID here;
my $limit = $tags->{Random_Limit} || 3;
my (@output, $sth);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
$sth = $link_db->select ( { CategoryID => $cat_id });
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
return { Random_Loop => \@output }
}

Then just substitute the bold bit with the category id you want to pull from.

Last edited by:

RedRum: Dec 26, 2001, 1:20 PM
Quote Reply
Re: [RedRum] Random Links Specified Per Category (continued ... ) In reply to
Hey thanks RedRum!

A question: would this mean that I'd need to set up a Gobal per category?

I think the way Alex had suggested it before, allowed me to set up one Global and call it different ways per category.

Also: if I tried what you suggest, can I list multiple categories within that global (for calls from the home page, for example where I'd want to show randoms from multiple categories, but not ALL categories)

Quote Reply
Re: [Evoir] Random Links Specified Per Category (continued ... ) In reply to
Oh ok, I see what you're saying.

Hmm, in that case Alex's code should be ok.

Have you specified Random_CatID?


Quote Reply
Re: [RedRum] Random Links Specified Per Category (continued ... ) In reply to
Smile Thanks again.

In Reply To:
Have you specified Random_CatID?

Aren't I doing that when I put the following code in the Category.html page:

<%set Random_Category = 9%>

Isn't that supposed to set the category ID I want to call from?

And, if I want to call from multiple categories, would I just use:

<%set Random_Category = 9,11,12%>

BTW: the code Alex gave me above does
not work for me. The system still pulls from multiple catgories (Ones I didn't define) Unsure

Quote Reply
Re: [RedRum] Random Links Specified Per Category (continued ... ) In reply to
OKOK... so I'm a little slow.

what needs to happen is
<%set Random_CatID = 9%>

NOT

<%set Random_Category = 9%>

I feel like that was some sort of test or something... Wink Oh well. I'm glad to have figured it out, finally. This is a really nice Global. Thanks Alex and RedRum.

final Global:

Code:
sub {
my $tags = shift;
my $link_db = $DB->table('Links','CatLinks');
my $cat_id = $tags->{Random_CatID};
my $limit = $tags->{Random_Limit} || 3;
my (@output, $sth);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
if ($cat_id) {
$sth = $link_db->select ( { CategoryID => $cat_id });
}
else {
$sth = $link_db->select;
}
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
return { Random_Loop => \@output }
}

Final html code for category template:
Code:
<%set Random_CatID = 9%>
<%Random_Link%>
<%loop Random_Loop%>
<p><%body_font%><A href="<%db_cgi_url%>/detail_page.cgi?ID=<%ID%>"><b><%Title%></b><br></A> <%Description%>
[<A href="<%db_cgi_url%>/detail_page.cgi?ID=<%ID%>">more</A>]</%body_font%>
<br>
<%endloop%>

Quote Reply
Re: [Evoir] Random Links Specified Per Category (continued ... ) In reply to
I get the following error:

Code:
Can't call method "fetchrow_hashref" on an undefined value at (eval 51) line 14.

Any ideas?

Many thanks, and Happy New Year !


------------------------------------------
Quote Reply
Re: [DogTags] Random Links Specified Per Category (continued ... ) In reply to
That means $sth is undefined.
Quote Reply
Re: [DogTags] Random Links Specified Per Category (continued ... ) In reply to
Hi,

Which means you are using MySQL 3.22 most likely (if you type in SELECT VERSION() from the SQL Monitor you can find out for sure). This global will only work on 3.23 as it uses ORDER BY RAND().

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Random Links Specified Per Category (continued ... ) In reply to
Thank you very much.

I'll try that query pronto.

------------------------------------------
Quote Reply
Re: [Alex] Random Links Specified Per Category (continued ... ) In reply to
ok... another question about this:

I'd like the static pages to have random links (dynamic) BUT, I want those links to link to static pages, not dynamic pages. I tried creating a template called "random.html"

in random.html I put the following code:
Code:
<%Random_Link%>
<%loop Random_Loop%>
<p>
<%body_font%>
<a href="<%build_root_url%>/Detailed/<%ID%>.html"><b>
<%Title%>
</b><br>
</A>
<%Description%>
<a href="<%build_root_url%>/Detailed/<%ID%>.html"> more</A></%body_font%>
<br>
<%endloop%>
I then called it by using this include:
Code:
<!--#include virtual="/cgi-bin/links/page.cgi?p=random"-->

3 questions:

1) Is there another way to call the detailed pages in random.html so that they point to the static pages? I would rather have folks read the static pages than the cgi pages. I figured this part out and edited above in bold. silly me

2) I found that I had to put the random.html in the default folder rather than the local folder. What would be the way to call it from the local folder?

3) I'd like to be able to identify the catgegory when I make the ssi call like this:
Code:
<%set Random_CatID = 9%><!--#include virtual="/cgi-bin/Links/page.cgi?p=random"-->
But it doesn't work. This means that I need to create a random.html file for each category. I'd like to streamline this process. I'd be happy to set it up inside the category templates (I'm using multiple template sets) but then the random links do not rotate, they are static until I rebuild.

Can you help me figure out a way to make this work? Am I explaining what I'm looking for clearly?







Last edited by:

Evoir: Dec 28, 2001, 12:57 PM
Quote Reply
Re: [Alex] Random Links Specified Per Category (continued ... ) In reply to
Bulls-eye, Alex

3.22.32

------------------------------------------
Quote Reply
Re: [Evoir] Random Links Specified Per Category (continued ... ) In reply to
Try adding d=0 to the parameter string passed to page.cgi




PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Random Links Specified Per Category (continued ... ) In reply to
pugdog,

can you give me an example of what you mean? I don't understand Unimpressed

<%set Random_CatID = 9%><!--#include virtual="/cgi-bin/Links/page.cgi?p=random"-->

Where would I add it? How would it work?



Last edited by:

Evoir: Dec 29, 2001, 12:52 PM
Quote Reply
Re: [Evoir] Random Links Specified Per Category (continued ... ) In reply to
Try:

<%set Random_CatID = 9%><!--#include virtual="/cgi-bin/Links/page.cgi?d=0&p=random"-->



PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Random Links Specified Per Category (continued ... ) In reply to
Hey Pugdog,

This didn't work. I'm getting random's from all categories. It's like the include doesn't recognize the <%set Random_CatID = 9%> Unimpressed

hmmm. Any other ideas? (what does the d=0 do, anyway?)

Quote Reply
Re: [Evoir] Random Links Specified Per Category (continued ... ) In reply to
I was only addressing your "static" vs "dynamic" links issue :)

I'm not up to tackling the other one :)


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [Evoir] Random Links Specified Per Category (continued ... ) In reply to
Hi,

You need to get a firm understanding of SSI, CGI and HTML pages to understand what is going on why.

Consider:

1. When you run Build All, the program generates a single html page for every category. Links SQL takes your category.html template, parses it with information about the category, and then saves the output as an html page. Now, when people view this page, they are viewing a static html page, the page does not get parsed again by Links SQL, so any template tags are useless (and will have already been removed).

2. However, if you have SSI turned on, then Apache will parse the page and run any commands it finds.

See the problem? You have <%set CategoryID = 5%>, however this is only used by Links SQL, and only evaluated once when the pages are built. Apache does not see this at all.

3. Now CGI generated pages are a bit different, Apache does not parse the output of CGI, so you can not use SSI on them. That's why <!--#include ...--> won't work on the search_results template, but would on the category.html template.

As to your question, what you want to do is pass CategoryID into the SSI call. So you want your parsed page to contain:

<!--#include virtual="/page.cgi?p=random&CategoryID=5"-->

To do that you can either hard code the number in, or if you want it different for each category, use a template variable (as then each category page that gets built will have a different ssi command).

Hope that helps,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [pugdog] Random Links Specified Per Category (continued ... ) In reply to

Last edited by:

Evoir: Dec 29, 2001, 3:07 PM
Quote Reply
Re: [Alex] Random Links Specified Per Category (continued ... ) In reply to
Hey Alex,

Thanks. What you wrote makes sense. But it didn't seem to work.

My include: <!--#include virtual="/cgi-bin/Links/page.cgi?p=random&CategoryID=9"-->

My random.html: <%Random_Link%><%loop Random_Loop%>
<p><%body_font%><a href="<%build_root_url%>/Detailed/<%ID%>.html"><b><%Title%></b><br></A> <%Description%>
[<a href="<%build_root_url%>/Detailed/<%ID%>.html"> more</A>]<%/body_font%>
<br>
<%endloop%>

And I seem to get categories from across the board. Unsure


Quote Reply
Re: [Evoir] Random Links Specified Per Category (continued ... ) In reply to
Hi,

Sorry, I didn't look at your global, it has:

my $cat_id = $tags->{Random_CatID};

So your SSI should of course be:

<!--#include virtual="/page.cgi?p=random&Random_CatID=9"-->

not CategoryID.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Random Links Specified Per Category (continued ... ) In reply to
Wink Yippee! Thanks Alex and Pugdog.

If you still have the energy: how would I make the category id be dynamic, so I don't have to code every category by hand?

And thanks a ton! Sly

Quote Reply
Re: [Evoir] Random Links Specified Per Category (continued ... ) In reply to
Hi,

You could do:

<!--#include virtual="/page.cgi?p=random&Random_CatID=<%ID%>"-->

as <%ID%> will be replaced by the ID number of the category.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Random Links Specified Per Category (continued ... ) In reply to
Alex,

Thank you, thank you. I understand so much more about this stuff. I'll still probably ask stupid questions, but your explanations are very helpful.

Smile Yippee! Thanks!