Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

LastLink SQL?

Quote Reply
LastLink SQL?
You might know the LstLink mod for Links2, which graps to most recently added links and puts them on the main homepage.
Has anybody managed to convert this to LinksSQL?

John
Quote Reply
Re: LastLink SQL? In reply to
That would be done something like the "top5" mod I just posted for search, except instead of the search terms database, you'd search the links database for the most recently added files, limit to whatever # you want, and put that into a variable in the HTML_Templates.pm %GLOBALS hash and you can use it anywhere on an HTML output page.

You'd probably just want to list the titles with the ..../jump.cgi?ID=nn sort of link around it.

Quote Reply
Re: LastLink SQL? In reply to
hi john

i thought i'd be a little help..

ok.. well.. this is for the nph-build.cgi.. if you even care about page.cgi i'll tell you now it's similiar.. i don't use page.cgi so i don't care.. =]

sub build_home_page

Code:
my $sth = $db->prepare("SELECT * FROM Links ORDER BY Add_Date DESC LIMIT 1");
$sth->execute() or die $DBI::errstr;
my $rec = $sth->fetchrow_hashref;

now $rec is a hashref of the last link added to the database.. SOO.. after this you would usually put it into a HTML_Templates.pm sub..

after that..
Code:
my $lastlink = &site_html_lastlink ($rec);

then look for..

Code:
print HOME &site_html_home ({

after this.. add

Code:
lastlink => $lastlink,

now it should work as <%lastlink%>

also.. remember to make the subroutine in HTML_Templates.pm

and don't forget to add the subroutine's name in the @EXPORT.. =]

jerry
Quote Reply
Re: LastLink SQL? In reply to
gotze.. can you change the url to mylinks.cgi to..

http://www.gossamer-threads.com/...um3/HTML/004046.html

and in the description add..

Quote:
source: http://jsu7785.hypermart.net/mylinks.txt

thanks! ;0)

sorry.. too lazy to go to the resource center

jerry
Quote Reply
Re: LastLink SQL? In reply to
You could also do a:

my $sth = $db->prepare("SELECT * FROM Links ORDER BY ID DESC LIMIT 1");

And change the limit to whatever you wanted. The ID order is going to always be more accurate than date order, since it's _possible_ two links could have the same add date, they will never have the same ID.

The down side is that if you change the add date manually, for some reason, the sort will still be by ID ... which might actually be an advantage in some cases.

Quote Reply
Re: LastLink SQL? In reply to
Thanks Jerry and pugdog,

I get Software error:
Compilation failed in require at nph-build.cgi line 33
when trying to add stuff. Of course I realise that I add the wrong stuff the wrong places ...

I guess I understand what you're doing, but I have no idea what to put where (my perl skills are next to none, but I'm good at copy-and-pasing). Would you please take a moment and explain what to put where (including the sub in HTML_Templates.pm)?

Jerry, I have updated the resource center.
Quote Reply
Re: LastLink SQL? In reply to
OK, going back to this mod, if you don't mind Blush

I modified your top5 code to this:
Code:
sub lastlink {
my $in = new CGI;
my $search = new Links::DBSQL $LINKS{admin_root_path} . "/defs/Links.def";

my $results = $search->query ( { ID => '*', sb => 'Add_Date', so => 'DESC', mh => 5 } );
my $output = '';
my $term_q;
if ($search->hits) {
foreach my $term (@{$results}) {
$term = $search->array_to_hash($term);
$term_q = $in->escape($term->{Term});
$output .= &site_html_link($term);
}
}
else {
$output = "Sorry not enough links yet.";
}
return $output;
}
I added
Code:
lastlink => \&lastlink
to the globals.

Well. I works. Sort of ... It does present the last links, but all of a sudden, I get:
"Unkown Tag: category
There are Unkown Tag: grand_total links for you to choose from!"
See http://www.governments-online.org/

Any suggestions? Please Smile

John

Quote Reply
Re: LastLink SQL? In reply to
Hmmm ....

If I move the <%lastlink%> in the home.html down under the <%category%> and <%grand_total%>, they are no longer unknown tags.

Strange. I guess I can design some tables and live with this peculiarity, but it would be nice to be able to use <%lastlink%> anywhere ...

Quote Reply
Re: LastLink SQL? In reply to
Are you sure you didn't have some odd HTML code? The way the tags are processed that seems very odd that one tag would interfere with other tags that are not in any way dependent on it -- unless you are clearing some values unintentionally?

You might also consider:

lastlink => \&lastlink

removing the "/", which makes it a "reference" to the subroutine rather than the results of the call. When the page is built, &lastlink will be called when first initialized, but won't be re-initialized.

Unlike a "random number" you really don't need to have the &lastlink routine run more than once per build.

Don't know if that makes a difference in anything, but since you are trying to stuff a value, I'm just wondering why you used a reference.

http://www.postcards.com
FAQ: http://www.postcards.com/FAQ/LinkSQL/

Quote Reply
Re: LastLink SQL? In reply to
Eureka! It was indeed the \ in
lastlink => \&lastlink
that gave the problem.

I was actually wondering what the \ was for ... I found it in your code, and just used it too. Guess I should have looked up the meaning of \& in my Perl book Blush Well, thanks for explaing it for me pugdog.

Now I am just wondering whether this will work with the hopefully-coming-very-soon new release ...


Quote Reply
Re: LastLink SQL? In reply to
Frown
I spoke too soon. When removing the \ reference, I got other Unknow tags, now in the lastlink (in the link only, description and ID is OK).
So, $output .= &site_html_link($term) is not working.
And $output .= qq~ <li><a href="/cgi-bin/sql/jump.cgi?ID=$term_q">${$term}{ID}</a></li> works, but only gets the ID, not the description.

I throw in my towel ...

Well, I decided to give Widgetz method a try, and I got that working, but as it only takes the most recently added link, I wanted to modify it to get more. II tried using LIMIT 3 in the LINKDB prepare statement, but this is obviously not enough. And in trying to write a foreach rountine, I ran into problems, and got stuck.

Frown


Quote Reply
Re: LastLink SQL? In reply to
gotze,

Did you change the LIMIT value in the following codes:

Code:

my $sth = $db->prepare("SELECT * FROM Links ORDER BY Add_Date DESC LIMIT 1");


Did you try changing 1 to a higher number??

Regards,

Eliot Lee

Quote Reply
Re: LastLink SQL? In reply to
Yes, I did. The problem is that my $rec = $sth->fetchrow_hashref only fetches one row. So it needs to be rewritten with a foreach statement, I suppose.

I tried looking at some of Alex' old code (http://www.gossamer-threads.com/p/68165#Post68165) and came up with:
Code:
my $hits = $LINKDB->prepare("SELECT * FROM Links ORDER BY ID DESC LIMIT 3");
if ($LINKDB->hits) {
foreach my $link (@{$hits}) {
my $link = $hits->array_to_hash($link);
my $lastlink .= &site_html_link ($link);
}
}
but that gives no output (nor any errors though).

Any ideas?

Quote Reply
Re: LastLink SQL? In reply to
my $lastlink;
my $sth = $LINKDB->prepare("SELECT * FROM Links ORDER BY ID DESC LIMIT 3");
$sth->execute() or die $DBI::errstr;
while (my $rec = $sth->fetchrow_hashref) {
$lastlink. = &site_html_link ($rec);
}

Jerry Su
Quote Reply
Re: LastLink SQL? In reply to
Cool
Thanks Jerry!

$lastlink. = &site
is of course
$lastlink .= &site
but besides that - PERFECT!

I wish they had Perl classes when I went to highschool ... Wink