Gossamer Forum
Home : Products : Links 2.0 : Customization :

Pull one entry from one category and display on home page.

Quote Reply
Pull one entry from one category and display on home page.
I posted this before, but it must not have made much sense, because no one responded! Let me try again...

I've got one category that I use for a hall of fame type of setup... what would be nice would be if I could take one of the listings from the hall of fame and bring it forward to the home page for added exposure. So, each time I rebuild the index, a new listing would be brought to the home page.

I've seen a mod that will pull a random link from the entire database, but I would like to focus that random link on one category only...

Is this a tough thing to do?

Thanks for any and all help... I'm stuck!!!

marc
Quote Reply
Re: [mb] Pull one entry from one category and display on home page. In reply to
Is this possible through some modification to the site of the day mod? I've learned how to randomize and include the site of the day into your homepage through templates... just can't figure out how to do this from one category only.

Any help is surely appreciated!

marc
Quote Reply
Re: [mb] Pull one entry from one category and display on home page. In reply to
Pretty please? I promise I won't ask for anything else ;-)
Quote Reply
Re: [mb] Pull one entry from one category and display on home page. In reply to
Just wanted to bring this one back up to the top. Still desperately needed. I can't even find anyone to do this for pay!

marc
Quote Reply
Re: [mb] Pull one entry from one category and display on home page. In reply to
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 &amp;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&amp;cgierr("unable to open output file for random links: $db_url_name. Reason:$!");

if ($db_use_flock) { flock (OUTPUTFILE, 2) or&amp;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 =&amp;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,(.*)/[^/]+,) &amp;&amp; unshift(@INC, "$1"); # Get the script location: UNIX/
($0 =~ m,(.*)\\[^\\]+,) &amp;&amp; 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 { &amp;main;}; # Trap any fatal errors so the program hopefully
if ($@) {&amp;cgierr("fatal error: $@"); } # never produces that nasty 500 servererror page.
exit; # There are only two exit calls in the script,here and in in &amp;cgierr.

sub main {
#--------------------------------------------------------
my %in = &amp;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 &amp;error ("unable to open links database:$db_url_name. Reason: $!");
while(<INDEX>) {

(/^#/) and nextLINE;
(/^\s*$/) andnext LINE;
chomp;
@data =&amp;split_decode($_);
$find++ == $rand ornext;
%rec = &amp;array_to_hash (0,@data);
last;
}
closeINDEX;

$links .= &amp;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.





Quote Reply
Re: [avern] Pull one entry from one category and display on home page. In reply to
The formatting didn't turn out too well, but any line that starts with a"#" is just comment within the actual code. There are only 3 lines thatyou need to modify in the code, and they each are preceded by a comment thatsays "MODIDFY THE FOLLOWING LINE".
Quote Reply
Re: [avern] Pull one entry from one category and display on home page. In reply to
Thank you for taking the time to post a solution. I will test this out and let you know how it does for me :-)



marc
Quote Reply
Re: [mb] Pull one entry from one category and display on home page. In reply to
I forgot a very important part, though. Unsure Without the following addition to nph-build.cgi, the new file will never actually get created. Make the following change to nph-build.cgi in the sub build_all.

Right after:

# Rebuild URL Index (This file is auto-generated, you will never need to touch it!

print
"Building URL Index . . .\n";
&amp;build_url_index;
print
"Done.\n\n";

Add the following:

# Rebuild Random Index (This file is auto-generated, you will never need to touch it!

print
"<B>BUILDING RANDOM RECORD INDEX . . .</B>\n";
&amp;build_random_files;
print
"Done.\n\n";

Please let me know if you run into any problems.

Avern
Quote Reply
Re: [avern] Pull one entry from one category and display on home page. In reply to
Has anyone tried this mod? If anyone could try it and let me know if it works, I'll add it to the mods section of the resources. Please let me know, too, if you run into any problems so I can try to help.

Thanks,
Avern