My personal hack makes duplicate work for links, but it works for my site; somaybe we can get it to work for you, too. The idea is that it creates a separatepipe-delimited file listing all the records from the category that youwant. Then I point a specialized version of (Glennu's, I think)randlinks.cgi to pick a random number of entries from that new file.
In nph-build.cgi add:
sub build_random_files {
#--------------------------------------------------------
# This routine isbased off the sub "build_url_index" and builds a
# new file that lists allrecords in a specified category
my @values =();
open (DB, "<$db_file_name") or &cgierr("unable to open database:$db_file_name.\nReason:$!");
##################################################################
# MODIFY THE FOLLOWINGLINE #
# change the path in this line to the path of new file that will #
#hold the new list ofrecords #
##################################################################
open (OUTPUTFILE, ">/change/this/path/to/somefile.txt") or&cgierr("unable to open output file for random links: $db_url_name. Reason:$!");
if ($db_use_flock) { flock (OUTPUTFILE, 2) or&cgierr ("unable to get exclusive lock. Reason: $!");}
LINE: while (<DB>){
/^#/ and nextLINE; # Skip commentLines.
/^\s*$/ andnext LINE; # Skip blanklines.
chomp; # Remove trailing new line.
@values =&split_decode($_);
##################################################################
# MODIFY THE FOLLOWINGLINE #
# change the word "yourcategoryname" to the name of the category #
# youwant to randomly selectfrom #
##################################################################
if($values[$db_category] =~ "yourcategoryname"){
##################################################################
# NOTE ABOUT THE FOLLOWINGLINE #
# this writes one line of data about each pertinent record. Each #
#field is separated by the regular db_delimiter. You can pick #
#which fields to exclude or include in this new file, just make #
# sure youmimic the format of the regularlinks.db #
##################################################################
print OUTPUTFILE"$values[$db_key_pos]$db_delim$values[$db_title]$db_delim$values[$db_URL]$db_delim$values[3]$db_delim$values[$4]$db_delim$values[$5]\n";
}
}
close DB;
close OUTPUTFILE;
}
--------------------------------------------------------------
Put the follwing into a new file called randlinks2.cgi (or whatever you like)and put the file in the same directory as add.cgi and search.cgi. Makesure the file is CHMOD 755:
#!/usr/bin/perl
#############################################################
#
#Variationon Random Links Display mod from GB Resources
#For more links 2 mods visit
http://cgi-resource.co.uk #
#Designedfor use with Links 2 which is sharware and by
#gossamer-threads.com
#
#############################################################
#############################################################
#############################################################
# Required Librariers
#--------------------------------------------------------
eval{
($0 =~ m,(.*)/[^/]+,) && unshift(@INC, "$1"); # Get the script location: UNIX/
($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC,"$1"); # Get the script location: Windows \
require"admin/links.cfg"; # Change this to full path to links.cfg if you haveproblems.
require"$db_lib_path/db_utils.pl";
require"$db_lib_path/links.def";
$build_use_templates?
require"$db_lib_path/site_html_templates.pl":
require"$db_lib_path/site_html.pl";
};
if ($@) {
print"Content-type: text/plain\n\n";
print "Error includinglibraries: $@\n";
print "Make sure they exist, permissionsare set properly, and paths are set correctly.";
}
# ========================================================
eval { &main;}; # Trap any fatal errors so the program hopefully
if ($@) {&cgierr("fatal error: $@"); } # never produces that nasty 500 servererror page.
exit; # There are only two exit calls in the script,here and in in &cgierr.
sub main {
#--------------------------------------------------------
my %in = &parse_form();
my ($num, $file, $i, $find, $rand, $count,$found,@data);
$num = $in{'number'};
##################################################################
# MODIFY THE FOLLOWINGLINE #
# change the path in this line to the path of new file that you #
# specified in the random_links sub ofnph-build.cgi #
##################################################################
$file= "/change/this/path/to/somefile.txt";
$count = `wc -l $file`;
for ($i = 0; $i < $num; $i++) {
$find = 0; $rand = int (rand($count + 0.5));
($rand ==$count) and ($rand--);
open(INDEX, "<$file") or &error ("unable to open links database:$db_url_name. Reason: $!");
while(<INDEX>) {
(/^#/) and nextLINE;
(/^\s*$/) andnext LINE;
chomp;
@data =&split_decode($_);
$find++ == $rand ornext;
%rec = &array_to_hash (0,@data);
last;
}
closeINDEX;
$links .= &site_html_link(%rec) . "\n";
}
print "$links";
}
-------------------------------------------------------------
That should do it I think. Please let me know if it doesn't work for you;Itried to pull this out of other mods I integrated so it's my best guess at ageneralized version.
Like the original randlinks.cgi, you should be able to call it by linking toit: <%db_cgi_url%>/randlinks2.cgi?number=5 or by running the scriptvia SSI just by using the exec cgi command.
Good Luck,
Avern
PS: One think I just noticed it that I could have made things easier byspecifying the output filepath as a new variable in links.cfg. This shouldstill work though.