Gossamer Forum
Home : Products : Links 2.0 : Customization :

Bobsie: Question about Recommend It Mod

(Page 1 of 2)
> >
Quote Reply
Bobsie: Question about Recommend It Mod
Bobsie,

I your "Recommend It" Modification, you were able to "pull out" the title of the link from the database so that the variable $rec{'Title'} can be used in the script. I have two questions.

One. Can you tell me how I can do the same thing in any script (in this case, jump.cgi)? I just want to be able to use $rec{'Title'} and get the title of the link.

Does it have anything to do with this line of code?

Code:
%rec = &get_record($fields{'ID'});

Two. If the title returned by $rec{'Title'} is more than one word, I need to convert the spaces between the words into plus (+) signs. How can I do that? For example, the title is "My Web Site". I need to convert that into "My+Web+Site".

Please pardon me if I'm not making any sense at all. I'm not a programmer, and I'm just guessing my way through all this.

Thanks.

[This message has been edited by t_dog (edited July 11, 1999).]
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Yes, that is the code that gets the record and assigns it to the array variable %rec.

To convert the spaces in the record's title to plus signs, after you get the record, do this:

Code:
$rec_title = $rec{'Title'};
$rec_title =~ s/ /\+/g;

I hope this helps.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Hmm... It still doesn't work.

If I want to place

Code:
%rec = &get_record($fields{'ID'});
$rec_title = $rec{'Title'};
$rec_title =~ s/ /\+/g;

in jump.cgi, where do I put it? Can I put it in sub main right before

Code:
my %in = &parse_form();
my ($goto, $id, $delim, $time);

Also, when I need to use the title variable, I use $rec_title and not $rec{'Title'}, correct? I will be using $rec_title at the bottom of jump.cgi, where it says "# Now let's send the user to the url..". Is that OK?

Thanks for your help.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
The jump.cgi script does not access links.db. It accesses url.db which does not contain the record's title at all. The $goto variable is based on the record obtained out of url.db that has the 'ID' passed to jump.cgi (if not randomly selected).

Where you would need to use the record title is in link.html (for templates) or site_html_pl sub site_html_link (if no templates).

So, put this in sub site_html_link of site_html_templates.pl or site_html.pl after the my %rec = @-; line:

Code:
$rec_title = $rec{'Title'};
$rec_title =~ s/ /\+/g;

If not using templates, then change:

Code:
$build_detailed ?
($output = qq~<ul><li><a class="link" href="$build_detail_url/$rec{$db_key}$build_extension">$rec{'Title'}</a>~) :
($output = qq~<ul><li><a class="link" href="$build_jump_url?$db_key=$rec{$db_key}">$rec{'Title'}</a>~);

to

Code:
$build_detailed ?
($output = qq~<ul><li><a class="link" href="$build_detail_url/$rec{$db_key}$build_extension">$rec_title</a>~) :
($output = qq~<ul><li><a class="link" href="$build_jump_url?$db_key=$rec{$db_key}">$rec_Title</a>~);

If using templates, change this is site_html_templates sub site_html_link:

Code:
return &load_template ('link.html', {
detailed_url => "$db_detailed_url/$rec{'ID'}$build_extension",
%rec,
%globals
});

to

Code:
return &load_template ('link.html', {
rec_title => $rec_title,
detailed_url => "$db_detailed_url/$rec{'ID'}$build_extension",
%rec,
%globals
});

You can now use <%rec_title%> in link.html where you need it.

Quote:
Also, when I need to use the title variable, I use $rec_title and not $rec{'Title'}, correct? I will be using $rec_title at the bottom of jump.cgi, where it says "# Now let's send the user to the url..". Is that OK?

It would not work there and, even if it would, it woudln't matter because it will not be displayed. All the code there does is pass the location of where to go to the browser and the browser goes there. It would never see $rec_title or anything else you tried to display.

I hope this helps.

[This message has been edited by Bobsie (edited July 12, 1999).]
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
 
Code:
# Now let's send the user to the url..
$goto ?
print "Location: $goto\n\n" :
&error ("Record not found ($in{$db_key})");
}

to

Code:
# Now let's send the user to the url..
$goto ?
print "Location: http://domain.com/cgi-bin/FrameIt.cgi?url=$goto&title=This+is+where+$rec_title+goes\n\n" :
&error ("Record not found ($in{$db_key})");
}

Do you understand? I really don't know how to explain this. I don't need to display anything. I just need to script to replace the variable $rec_title with the actual title.

The easiest way to explain this is that I want jump.cgi to have the ability to understand that $rec_title refers to the title of the link just passed to it, just like your modified birdcast.cgi.

Thanks.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Yes I understand now that you have said why you want to do this. Would this also be done for random links or just links that are being clicked on on the category pages?
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
 
Quote:
Would this also be done for random links or just links that are being clicked on on the category pages?

I would be nice if I could do it with the random links, too. But are random links and normal links different? I thought both use the same $goto.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
They both use the same $goto but the $goto is built for random links in a different part of jump.cgi than for a normal link. Look at this code for the random link:

Code:
# Get the random line from the url lookup database.
srand;
$find = 0; $rand = int (rand ($count + 0.5)); ($rand == $count) and ($rand--);
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL> ) {
$find++ == $rand or next;
/\d+$delim(.+)/o or next;
$goto = $1;
last;
}
close URL;
$goto or &error ("Can't find random line: $rand.");

And this code for a normal link:

Code:
# Let's get the URL.
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL> ) {
(/^$id$delim(.+)/o) or next;
chomp ($goto = $1);
last;
}
close URL;
$goto or &error ("Can't find link id: $id");

Those are the two places where $goto is built. So it would be best to put the title in that code instead of at the bottom.

To get the title, at the top of sub main in jump.cgi, change this:

Code:
my %in = &parse_form();
my ($goto, $id, $delim, $time);

to read:

Code:
my %in = &parse_form();
my ($goto, $id, $delim, $time, $link_rec, $rec_title);

Change:

Code:
# Get the random line from the url lookup database.
srand;
$find = 0; $rand = int (rand ($count + 0.5)); ($rand == $count) and ($rand--);
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL> ) {
$find++ == $rand or next;
/\d+$delim(.+)/o or next;
$goto = $1;
last;
}

to read:

Code:
# Get the random line from the url lookup database.
srand;
$find = 0; $rand = int (rand ($count + 0.5)); ($rand == $count) and ($rand--);
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL> ) {
$find++ == $rand or next;
/\d+$delim(.+)/o or next;
$link_rec = &get_record($find);
$rec_title = $link_rec{'Title'};
$rec_title =~ s/ /\+/g;
$goto = "http://domain.com/cgi-bin/FrameIt.cgi?url=$1&title=$rec_title";
last;
}

Change:

Code:
# Let's get the URL.
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL> ) {
(/^$id$delim(.+)/o) or next;
chomp ($goto = $1);
last;
}

to read:

Code:
# Let's get the URL.
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL> ) {
(/^$id$delim(.+)/o) or next;
chomp;
$link_rec = &get_record($find);
$rec_title = $link_rec{'Title'};
$rec_title =~ s/ /\+/g;
$goto = "http://domain.com/cgi-bin/FrameIt.cgi?url=$1&title=$rec_title";
last;
}

That should be all you need to do. You shouldn't have to change the Location: line.

I hope this helps.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Did you leave out this line?

Code:
%rec = &get_record($fields{'ID'});

Because $rec_title is not returning anything (that is, it returns a blank). Or does this

Code:
my ($goto, $id, $delim, $time, $link_rec, $rec_title);

take care of getting the record and puting it into the array?

The URL part works fine. It's just the title.

Thanks.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
In each of the parts of the code that I posted to create the $goto variable, there are three lines that say:

Quote:
$link_rec = &get_record($find);
$rec_title = $link_rec{'Title'};
$rec_title =~ s/ /\+/g;

I didn't forget it, I just changed the variable. However, I see I messed it up. Change all occurances of $link_rec to %link_rec. Let me know if it works.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
I just realiazed I made another boo boo....

Change this one:

Code:
# Let's get the URL.
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL> ) {
(/^$id$delim(.+)/o) or next;
chomp;
$link_rec = &get_record($find);
$rec_title = $link_rec{'Title'};
$rec_title =~ s/ /\+/g;
$goto = "http://domain.com/cgi-bin/FrameIt.cgi?url=$1&title=$rec_title";
last;
}

to read:

Code:
# Let's get the URL.
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL> ) {
(/^$id$delim(.+)/o) or next;
chomp;
%link_rec = &get_record($id);
$rec_title = $link_rec{'Title'};
$rec_title =~ s/ /\+/g;
$goto = "http://domain.com/cgi-bin/FrameIt.cgi?url=$1&title=$rec_title";
last;
}

Also, I said in my lost post to change all occurances of $link_rec to %link_rec... but DO NOT change the ones that say "$link_rec{'Title'}" as that needs to stay as a $.

[This message has been edited by Bobsie (edited July 12, 1999).]
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Hmm...

Now the title works. But for titles that have more than one word, the URL no longer works.

In other words, with a one word title everything works fine.

With a title of more than one word, this is the final output:

Code:
http://domain.com/cgi-bin/FrameIt.cgi?url=&title=Title+Here

So $1 is not returning a URL. The correct output should be:

Code:
http://domain.com/cgi-bin/FrameIt.cgi?url=http://urlhere.com/&title=Title+Here

[This message has been edited by t_dog (edited July 12, 1999).]
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Bobsie,

shouldn't you just do

framit.cgi?ID=##

so it could get the rec info that way.. would make it easier wouldn't it.. and people wouldn't be able to alter the title.. url or whatever..

k..bye
jerry
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Post the code you used so I can double check it.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
 
Quote:
Post the code you used so I can double check it.

Are you referring to me or Jerry? Smile
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
I am referring to your message, t_dog. Jerry's message wasn't there when I replied.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Here's my jump.cgi:

Code:
# 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 have problems.
require "$db_lib_path/db_utils.pl";
require "$db_lib_path/links.def";
};
if ($@) {
print "Content-type: text/plain\n\n";
print "Error including libraries: $@\n";
print "Make sure they exist, permissions are 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 server error page.
exit; # There are only two exit calls in the script, here and in in &cgierr.

sub main {
# --------------------------------------------------------
my %in = &parse_form();
my ($goto, $id, $delim, $time, %link_rec, $rec_title); # Terence added %link_rec, $rec_title

$id = $in{$db_key};
$delim = quotemeta($db_delim);
$time = time();

if ($id eq "random") {
my ($count, $rand, $find);

# Pull out the total number of links.
open (COUNT, "<$db_hits_path/index.count") or &error ("unable to open index count file: $db_hits_path/index.count. Reason: $!");
$count = int <COUNT>;
close COUNT;

# Get the random line from the url lookup database.
srand;
$find = 0; $rand = int (rand ($count + 0.5)); ($rand == $count) and ($rand--);
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL> ) {
$find++ == $rand or next;
/\d+$delim(.+)/o or next;
%link_rec = &get_record($find); # Added by Terence
$rec_title = $link_rec{'Title'}; # Added by Terence
$rec_title =~ s/ /\+/g; # Added by Terence
$goto = "http://freesources.com/cgi-bin/FrameIt.cgi?url=$1&title=$rec_title"; # Changed by Terence. Original: $goto = $1;
last;
}
close URL;
$goto or &error ("Can't find random line: $rand.");
}
elsif (exists $in{$db_key}) {
# Make sure this is a valid looking id.
($id =~ /^\d+$/) or &error ("Invalid id: $id");

# Let's get the URL.
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL> ) {
(/^$id$delim(.+)/o) or next;
chomp; # Changed by Terence. Original: chomp ($goto = $1);
%link_rec = &get_record($id); # Added by Terence
$rec_title = $link_rec{'Title'}; # Added by Terence
$rec_title =~ s/ /\+/g; # Added by Terence
$goto = "http://freesources.com/cgi-bin/FrameIt.cgi?url=$1&title=$rec_title"; # Added by Terence
last;
}
close URL;
$goto or &error ("Can't find link id: $id");

# Bump the counter one.
if (open (HIT, "<$db_hits_path/$id")) {
my ($count, $old_time, @IP, $ip, $visited);
chomp ($count = <HIT> );
chomp ($old_time = <HIT> );
chomp (@IP = <HIT> );
(($time - $old_time) > 21600) and (@IP = ());
foreach $ip (@IP) {
$ip eq $ENV{'REMOTE_ADDR'} and ($visited++ and last);
}
if (!$visited) {
push (@IP, $ENV{'REMOTE_ADDR'});
$count = $count + 1;
open (HIT, ">$db_hits_path/$id") or &error ("Can't open for output counter file. Reason: $!");
if ($db_use_flock) { flock (HIT, 2) or &error ("Can't get file lock. Reason: $!"); }
local $" = "\n";
print HIT "$count\n$time\n@IP";
close HIT;
}
}
else {
open (HIT, ">$db_hits_path/$id") or &error ("Can't increment counter file. Reason: $!");
print HIT "1\n$time\n$ENV{'REMOTE_ADDR'}";
close HIT;
}
}
else {
&error ("No link specified!");
}

# Now let's send the user to the url..
$goto ?
print "Location: $goto\n\n" :
&error ("Record not found ($in{$db_key})");
}

sub error {
# ------------------------------------------
#
print "Content-type: text/plain\n\n";
print "Error: $_[0]\n";
exit;
}

Thanks for going into all this trouble.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Jerry,

Quote:
shouldn't you just do

framit.cgi?ID=##

so it could get the rec info that way.. would make it easier wouldn't it.. and people wouldn't be able to alter the title.. url or whatever..

I don't see how that would call up the record so that he could get the record's title.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
t_dog,

I suspect I know what is causing that problem (and I suspected it might right from the start), so try this:

Change:

Code:
my ($goto, $id, $delim, $time, %link_rec, $rec_title);

to read:

Code:
my ($goto, $id, $delim, $time, %link_rec, $go_url, $rec_title);

Change this:

Code:
while (<URL> ) {
$find++ == $rand or next;
/\d+$delim(.+)/o or next;
%link_rec = &get_record($find); # Added by Terence
$rec_title = $link_rec{'Title'}; # Added by Terence
$rec_title =~ s/ /\+/g; # Added by Terence
$goto = "http://freesources.com/cgi-bin/FrameIt.cgi?url=$1&title=$rec_title"; # Changed by Terence. Original: $goto = $1;
last;
}

to read:

Code:
while (<URL> ) {
$find++ == $rand or next;
/\d+$delim(.+)/o or next;
$go_url = $1;
%link_rec = &get_record($find); # Added by Terence
$rec_title = $link_rec{'Title'}; # Added by Terence
$rec_title =~ s/ /\+/g; # Added by Terence
$goto = "http://freesources.com/cgi-bin/FrameIt.cgi?url=$go_url&title=$rec_title"; # Changed by Terence. Original: $goto = $1;
last;
}

And change:

Code:
while (<URL> ) {
(/^$id$delim(.+)/o) or next;
chomp; # Changed by Terence. Original: chomp ($goto = $1);
%link_rec = &get_record($id); # Added by Terence
$rec_title = $link_rec{'Title'}; # Added by Terence
$rec_title =~ s/ /\+/g; # Added by Terence
$goto = "http://freesources.com/cgi-bin/FrameIt.cgi?url=$1&title=$rec_title"; # Added by Terence
last;
}

to read:

Code:
while (<URL> ) {
(/^$id$delim(.+)/o) or next;
chomp ($go_url = $1); # Changed by Terence. Original: chomp ($goto = $1);
%link_rec = &get_record($id); # Added by Terence
$rec_title = $link_rec{'Title'}; # Added by Terence
$rec_title =~ s/ /\+/g; # Added by Terence
$goto = "http://freesources.com/cgi-bin/FrameIt.cgi?url=$go_url&title=$rec_title"; # Added by Terence
last;
}

I hope this fixes it.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Works perfectly!

Thank you so much, Bobsie. You are the greatest. Smile
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Oops. There's something wrong with the "random link" code. It is sending me to a random link but pulling the wrong title.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
forget this post... must have been dated in the 1960s...

[This message has been edited by widgetz (edited July 12, 1999).]
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
t_dog,

For the random link, try this. Change:

Code:
while (<URL> ) {
$find++ == $rand or next;
/\d+$delim(.+)/o or next;
$go_url = $1;
%link_rec = &get_record($find); # Added by Terence
$rec_title = $link_rec{'Title'}; # Added by Terence
$rec_title =~ s/ /\+/g; # Added by Terence
$goto = "http://freesources.com/cgi-bin/FrameIt.cgi?url=$go_url&title=$rec_title"; # Changed by Terence. Original: $goto = $1;
last;
}

to read:

Code:
while (<URL> ) {
$find++ == $rand or next;
($id, $go_url) = split /\Q$db_delim\E/;
%link_rec = &get_record($id); # Added by Terence
$rec_title = $link_rec{'Title'}; # Added by Terence
$rec_title =~ s/ /\+/g; # Added by Terence
$goto = "http://freesources.com/cgi-bin/FrameIt.cgi?url=$go_url&title=$rec_title"; # Changed by Terence. Original: $goto = $1;
last;
}

Let me know how it goes.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Now jump.cgi gives me a "500 Internal Server Error" when I access a random link.
Quote Reply
Re: Bobsie: Question about Recommend It Mod In reply to
Eliot,

We are talking about a completely different issue. Smile

The title of this topic says Recommend It Mod because I wanted Bobsie to tell me how I can use part of the code of his mod to jump.cgi. It no longer has anything to do with birdcast.cgi.

However, I still have to thank you for your post. Because, by pure coincidence, you answered a question about birdcast.cgi that I was about to ask after this problem with jump.cgi is resolved.

Still, you really should read what the thread is about before you reply. I make the same mistake sometimes. Smile
> >