Gossamer Forum
Home : Products : Links 2.0 : Customization :

EZ Random Link Mod by Kerouac

Quote Reply
EZ Random Link Mod by Kerouac
I needed a simple script to pull data from links.db and this is what I came up with. It's short and sweet and can be modified to pull ANY fields from your links.db and output it via ssi any way you like. As it is here, you'll get a random linked title. Enjoy.

#!/usr/local/bin/perl
# Name: EZ Random Link by Kerouac
# Date: 6-05-01
# File Name: ezrand.cgi
#
# Required: Links 2, Perl, SSI enabled pages
#
# What it does:
# This is a very simple script that pulls a random link from your links.db via ssi
# and displays it. It doesn't "require" any links files - it just randomly selects a link
# from links.db and puts it wherever you want it to appear on your site via ssi.
#
# Directions:
#
# 1. Change perl path to match yours
# 2. Put your system path in the $rand_file variable
# 3. Put the url to your Links jump.cgi script in the $cgi_url variable
# 4. Put your preferred font size in $fsize and your preferred font face in $fface
# 5. Decide if you want a linked title or a linked title plus description.
# This script is setup to return a linked title only. If you also want a description,
# then just comment out the "linked title only" print line and uncomment the
# "linked title with description" print line.
# 6. Make sure the $links_field[x] variables match your links.def file's fields.
# This script assumes that your relevant links.def fields are:
# 0 = ID
# 1 = Title
# 5 = Description
# 7. Upload in ascii mode to the directory where your links jump.cgi script is
# 8. Chmod to 755
# 9 Call via ssi as

$rand_file = "/usr/home/path/to/your/links/bin/admin/data/links.db";
$cgi_url = "http://yourdomain.com/links/bin/jump.cgi";
$fsize = "1";
$fface = "verdana,arial,helventica";

open (DATABASE, "<$rand_file") || die print "Could not open Links database";
@line=<DATABASE>;
close (DATABASE);
srand;
$array = $line[int rand(@line)];

@links_field = split (/\|/, $array);

print "Content-type: text/html\n\n";

# LINKED TITLE ONLY : The following line outputs a linked title only
print "<P><b><font size=\"$fsize\" face=\"$fface\"><a href=\"$cgi_url?ID=$links_field[0]\">$links_field[1]</a></font></b></p> ";

# LINKED TITLE WITH DESCRIPTION : Uncomment the following line and comment the above print line if you want a linked title PLUS description
# print "<P><font size=\"$fsize\" face=\"$fface\"><b><a href=\"$cgi_url?ID=$links_field[0]\">$links_field[1]</a></b>: $links_field[5]</font></p> ";


Quote Reply
Re: EZ Random Link Mod by Kerouac In reply to
Or how about requiring the Links2 files at the top and then you can use

split_decode();

...and other Links2 routines.

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: EZ Random Link Mod by Kerouac In reply to
I wanted to keep this a hugely simple without requiring any other scripts. It's very efficient, independant, and simple - and that's what I like in an ssi script that's going to be pulled so often for such a simple task.

But yes - this little script can be beefed up and modified in a ton of ways for a ton of different purposes. As it stands now, you can randomly pull any links.db field and output it any way you want. It wouldn't be too tough to get links subs and templates involved.... But it's so easy to modify the info.cgi mod into a print.cgi "printable version page" etc.. for all sorts of different record displays that use templates and links subs..

I did call it EZ random link - and it sure is :)



Quote Reply
Re: EZ Random Link Mod by Kerouac In reply to
You can scratch efficient off your list. You should NEVER slurp the whole database into memory like that. Just visit the CGI/Perl forum; GClemons and Mark would shoot you :-).

--Drew
Free, hot camel soup for Links hackers...
http://www.camelsoup.com
Quote Reply
Re: EZ Random Link Mod by Kerouac In reply to
Thats why I said he should use the Links2 routines....

Code:
LINE: while (<DB>) {
/^#/ and next LINE;
/^\s*$/ and next LINE;
chomp;
@values = &split_decode($_);

etc..

}
Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: [RedRum] EZ Random Link Mod by Kerouac In reply to
Hi

Can you provide the full script with your modification?

In other words, where do you stick your lines in the EZ random links?

Thanks