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

Link of the day..

(Page 1 of 2)
> >
Quote Reply
Link of the day..
Hi

I think that will be easy just could not figure out how to put it up.
How can I make a random link to be used on regular pages and dynamic pages?
and the same with a category of the day..

Regards
Abd

http://www.idleb.com
Quote Reply
Re: Link of the day.. In reply to
Hi,

Add a global:

random_link =>
Code:
sub {
my $db = $DB->table('Links');
my $total = $db->count ( 'isValidated' => 'Yes' );
my $rand = int( rand($total) );
$db->select_options ("LIMIT $rand, 1");
my $link = $db->select->fetchrow_hashref;
my $html = Links::SiteHTML::display('link', $link);
return $html;
}
You can now just put <%random_link%> on a page and it will print out a random link.

Cheers,

Alex

--
Gossamer Threads Inc.

Last edited by:

Inertia: Nov 4, 2008, 3:49 PM
Quote Reply
Re: Link of the day.. In reply to
 
What about having 3 random links (and make it so they don't duplicate). I thought, hey, I can just add <%random_link%> 3 times, but I don't want them to ever be doubles listed.

Alex?

Quote Reply
Re: Link of the day.. In reply to
One other thing: this seems to not be a dynamic link under the html pages. Is there a way to make this pull from the db each time on a non cgi page?


Quote Reply
Re: Link of the day.. In reply to
 
Noooooo..... not unless you are using SSI or <eek!> PHP <G>



PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: Link of the day.. In reply to
I would happily use SSI or PHP to accomplish this. :)

What I'd like to do is to just have 3 random links (or stories in my case) on the Links home page. (Stories page in my case). I'd like the stories to update each time the page is loaded.

Can you tell me how I can do this via SSI? (My pages are parsing for SSI already) I just don't know what I would call.

Quote Reply
Re: Link of the day.. In reply to
Hi,

Easiest thing to do would be:

1. Add a template called 'random.html' that is formatted how you want the random links to show up. It could be as simple as:

<li><%random_link%>
<li><%random_link%>
<li><%random_link%>

or more complex.

2. Add an SSI that does:

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

and that's it!

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Link of the day.. In reply to
Thanks Alex! This works perfectly.


Quote Reply
Re: [Alex] Link of the day.. In reply to
Hey Alex,

So, this works pretty well, but I do sometimes get duplicates showing up at the same time. Is there a way to specify NO duplicates?

Or,

Is there a way to say "pull from category a,b,c" in one ssi call, and in another ssi call "pull from category d,e,f" etc

Or, maybe there is another approach I am not thinking of.

Can you help me figure this one out?
Quote Reply
Re: [Alex] Link of the day.. In reply to
Hi All

What if you wanted to show just the Title of a random link? What would the sub look like?

I tried the sub by Alex, and then I put in the following loop:

Code:
<%random_link%>
<%loop random_link%>
<%Title%><BR>
<%endloop%>
But, I still got all of link.html, not just the Title.

Many thanks Smile

DT

Last edited by:

DogTags: Sep 23, 2001, 9:43 PM
Quote Reply
Re: [Alex] Link of the day.. In reply to
Trying again here:

I do sometimes get duplicates showing up at the same time. Is there a way to specify NO duplicates?

Is there a way to say "pull from category a,b,c" in one ssi call, and in another ssi call "pull from category d,e,f" etc

Or, maybe there is another approach I am not thinking of.

Can you help me figure this one out?

Please?
Quote Reply
Re: [Alex] Link of the day.. In reply to
Alex, can you help me with this?

Last edited by:

Evoir: Oct 5, 2001, 7:31 PM
Quote Reply
Re: [Alex] Link of the day.. In reply to
I'm not sure what to do. Alex, you gave me a suggestion that works, but it also allows duplicates of the links, which turns out to not be a great solution. IS there some way to tell the system to pull from different categories for each random link, or to simply tell the system to give NO DUPLICATES?

Quote Reply
Re: [Alex] Link of the day.. In reply to
Ok,

I have realized what I really want is:

1.) to be able to insert a random link per category (that I define in the call)

So, I can use this random link per category elsewhere on the site, as well as on my Links main page. It seems like this would be something other folks would want to do as well, no?

2.) random link from entire db. This I might call on the front page of my entire website. a way to pull folks into the stories section. (I am using LinksSQL to run stories on my site)

can anyone explain how I'd do these two things??

Last edited by:

Evoir: Oct 6, 2001, 12:25 PM
Quote Reply
Re: [Evoir] Link of the day.. In reply to
Hi,

This is getting more complex. You want something like:

Code:
Random_Link => 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 }
}

This will only work with MySQL 3.23, as you can't do ORDER BY RAND() on older mysql's. To use this you would put:

<%Random_Link%>
<%loop Random_Loop%>
Random: <%Title%>: <%URL%><BR>
<%endloop%>

or if only for a specific category, you would do:

<%set Random_Category = 3%>
... rest as normal.

Hope this helps,

Alex
--
Gossamer Threads Inc.

Last edited by:

Alex: Nov 21, 2001, 5:52 PM
Quote Reply
Re: [Alex] Link of the day.. In reply to
Hey Alex,

Just tried your fix, and it doesn't work Unsure

here's what happens when I try and build after adding what you suggested:

Code:
A fatal error has occured:


Unable to compile 'Random_Link'. Reason: syntax error at (eval 17) line 17, at EOF
syntax error at (eval 17) line 18, near "}
}"

ideas, thoughts?

Quote Reply
Re: [Evoir] Link of the day.. In reply to
Try it now, I had a syntax error in it..

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Link of the day.. In reply to
Wink Alex,

ok, it "works" now, but not exactly how i'd like it to. I'd like to make it so I can include the description, and a link to the detailed story. Perhaps the thumbnail image. I think the first or second example in this thread worked, it just gave duplicates.

The fields I'd like it to pull from are:

Title (linked to the detailed page)
Description (I'm using it as a synopsys)
Image_Thumbnail (linked to the detailed page)

I'd like it to pull x # of articles (or link) from the categories I state, and show no duplicates.

Does this help clarify? can you help me with this?


Quote Reply
Re: [Evoir] Link of the day.. In reply to
Quote:
I'd like it to pull x # of articles (or link) from the categories I state, and show no duplicates.

Basically, what you will have to do is add another table called something like Links_SiteDay. This table should contain the following columns:

Quote:

LinkID (INT, PRIMARY, NOT NULL)
DayCreated (DATE, NOT NULL, NOW())



Then in the above sub that defines the global, you can add a reference to another sub or add codes within it to first, ADD a row with the LinkID chosen and the Date. Then add additional check codes to check the Links_SiteDay table to make sure that the link is not in that table.

I've done this with LINKS SQL v.1.3, but the codes, of course, are not portable to the current version of LNKS SQL.

OR add another column in the Links table called something like isSiteDay (ENUM, 'Yes,'No', NOT NULL, No). Again, you will have to write additional codes that will UPDATE this column when a LinkID is selected. The column should be updated to Yes if chosen. Then add additional check codes in the above query that Alex provided.

========================================
Buh Bye!

Cheers,
Me

Last edited by:

Heckler: Nov 21, 2001, 6:18 PM
Quote Reply
Re: [Alex] Link of the day.. In reply to
I actually really like what Alex first showed us
Code:
sub {
my $db = $DB->table('Links');
my $total = $db->count ( 'isValidated' => 'Yes' );
my $rand = int( rand($total) );
$db->select_options ("LIMIT $rand, 1");
my $link = $db->select->fetchrow_hashref;
my $html = Links::SiteHTML::display('link', $link);
return $html;
}

But, it makes duplicates, and doesn't allow for category control.

Alex, aint there some way to use the global variables and create output like the global variable I just showed you...but keeping duplicates out, and having category control? There has got to be. Unsure

Quote Reply
Re: [Evoir] Link of the day.. In reply to
Hi,

Just modify:

<%Random_Link%>
<%loop Random_Loop%>
Random: <%Title%>: <%URL%><BR>
<%endloop%>

to suit your needs. i.e.

<%Random_Link%>
<%loop Random_Loop%>
The title is: <%Title%>, the URL is <%URL%>, here it is <a href="<%URL%>"><%Title%></a> linked.
<%endloop%>

you can use any normal Link tag you like in there to format it however you want.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Link of the day.. In reply to
Thanks Alex. I'll give it a whirl this weekend. Smile
Quote Reply
Re: [Evoir] Link of the day.. In reply to
However, those codes do NOTHING to check for duplicates, thus that is why you have problems with duplicates showing.

That is WHY I recommended using another column in the Links table as a FLAG to ignore if that has already been chosen...

Got it??
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Alex] Link of the day.. In reply to
Alex,

does the code you provided keep duplicates from happening?
Quote Reply
Re: [Evoir] Link of the day.. In reply to
Yup, the order by rand() in mysql will not pull the same record twice.

Cheers,

Alex
--
Gossamer Threads Inc.
> >