Gossamer Forum
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.
Quote Reply
Re: [Alex] Link of the day.. In reply to
Where would the <%set Random_Category = 3%> be placed?

Would it be:

Code:
<%Random_Link%>
<%set Random_Category = 3%>
<%loop Random_Loop%>
Random: <%Title%>: <%URL%><BR>
<%endloop%>

Many thanks! Cool



------------------------------------------
Quote Reply
Re: [DogTags] Link of the day.. In reply to
No, it has to come before the call to <%Random_Link%>.

Cheers,

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

Still tweeking htis random thread thingy. I'm able to get it going, but it doesn't work dynamically on the static pages. What I'm hoping for is to have it work like the ssi option did (each page reload gives you a different set of links)

The html code I used is perfect, except for the dynamic issue on static pages. Is there a way around this?

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

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

Do the same thing as before, save the html you pasted to some template (lets call it random.html) and then add ssi that looks like:

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

and the SSI will parse and load the random.html template.

Hope that helps,

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

it works CoolBlushTongue

Alex. I like your mind.

I now understand so much more about LinksSQL. It's all in the global variables. The reason I have so much trouble, is that I don't know perl. You don't really have to ever "hack" LinksSQL, because you can do everything from within the program (if you know some perl). Very cool beans. (Although, I'm still gonna have to ask questions like these. Hopefully, it helps alot more people than just me.

This is soo friggin cool. Thanks.

Last edited by:

Evoir: Nov 23, 2001, 5:26 PM
Quote Reply
Re: [Alex] Link of the day.. In reply to
I've been still trying to implement this. here's what I've got.

I created a gobal called Random_Link:

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

Then I created a template called random.html:

Code:
<%set Random_Category = 9%>
<%Random_Link%>
<%loop Random_Loop%>
<p> <b><%body_font%><a href="<%URL%>"><%Title%></a></b> <%Description%>
[<a href="<%URL%>">more</A>]</%body_font%></p>
<%endloop%>

Then, I called this template from a seperate category page, that has been defined in the browser to be called for a certain category, let's say category 9.

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

It does not seem to differentiate and only send me items from category 9, instead, it sends me randoms from all the categories.

Perhaps there is still something missing. Unsure
Can you take a look Alex?

Last edited by:

Evoir: Dec 11, 2001, 11:47 PM
Quote Reply
Re: [Evoir] Link of the day.. In reply to
I tried Evoir's stuff, but I got an error message.

Everything was the same, except:

1. I called my template file randomssi.html instead of random.html (which I already had)

2. I dropped the random category piece of the loop for now and used:

Quote:
<%Random_Link%>
<%loop Random_Loop%>
<p> <b><%body_font%><a href="<%URL%>"><%Title%></a></b> <%Description%>
[<a href="<%URL%>">more</A>]</%body_font%></p>
<%endloop%>

Here is the error message that I got:

Quote:
A fatal error has occured:

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

Would anyone know where I am messing up?

Thanks.

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

can you help me figure out why it is not discriminating which category it pulls from? Smile

Quote Reply
Re: [Alex] Link of the day.. In reply to
pretty please? Blush

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

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

It works great, I just would like to add another condition...I would like the random links to be validated links Txs FMP
Quote Reply
Re: [Alex] Link of the day.. In reply to
Just wanted to add what's already pointed out here.

This global is pulling date from ALL categories regardless of the <%set Random_Category = 3%> setting.

Do we have a fix for this.

I sincerely hope so Unsure

--------------------------------
Privacy Software
Quote Reply
Re: [Alex] Link of the day.. In reply to
OK, something's obviously up with this very good global that’s preventing it from working as advertised.

The problem is it's supposed to list only from the category number, but actually lists from all categories. Perhaps it's something to do with <%set Random_Category = 3%> not being matched in the global? Sorry I don't have the perl know-how to figure this out!

Here it is unmodified:


<%set Random_Category = 3%>
<%Random_Link%>
<%loop Random_Loop%>
<%Title%>
<%endloop%>


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 }

}

--------------------------------
Privacy Software
Quote Reply
Re: [BLOOD] Link of the day.. In reply to
OK, I found another thread with similar code that works good.

But can this global be changed to allow a category and all subcategories below it to be listed from.

As it is you pass in individual categories like:

page.cgi?p=include&Random_CatID=6&Random_CatID=7

sub {
my $tags = shift;
my $limit = ($tags->{Random_Limit} =~ /^(\d+)$/) ? $1 : 3;
my @parents = $IN->param('Random_CatID');
my @cat_ids;
foreach my $id (@parents) {
push @cat_ids, $id, $DB->table('Category')->children($id);
}
my $link_db = $DB->table('Links','CatLinks');
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
my $condition = GT::SQL::Condition->new( 'Description', 'IS NOT', \'NULL','isValidated','=','Yes');
if (@cat_ids) {
$condition->add('CategoryID', 'IN', \@cat_ids);
}
my $sth = $link_db->select($condition);
my @output;
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
return { Random_Loop => \@output }
}

--------------------------------
Privacy Software
Quote Reply
Re: [BLOOD] Link of the day.. In reply to
On a first glance I would say that you need to either use Random_Category OR Random_CatID.

Try changing it to <%set Random_CatID = 3%>
Quote Reply
Re: [BLOOD] Link of the day.. In reply to
As far as I can see this should list from a category and all subcategories as it is. I think that the extra category in the example is just another category - not a subcategory - and it should then use all the subcategories of both category 6 and category 7.