Gossamer Forum
Home : General : Perl Programming :

Help with small script - finding related records

Quote Reply
Help with small script - finding related records
I posted this is the DBMan forum but since there isn't much activity from responders these days I'm hoping someone here can help me.

I'm using a little sub that extracts related records for the database and creates a list.

The sub is called from within my page display as:


print &other_title_link("$rec{'Title'}", "6"); (which is defining: print &other_field_link("$value", "$field_num"); )

The script works great for creating the lists unless the Title field contains and "&" and then no list is generated :(

Not being a programmer I've tried several attempts to get those records to display, but failed.

How can I get the script to convert the & to & or whatever it takes for the script to pull in and display those records.


sub other_title_link {
#----------------------------------------
## this displays related subtitles listed on static pages

my $value = shift;
my $field_num = shift;
my $link;
open (DB, "<$db_file_name") || &cgierr("Can't open $db_file_name in other_field_link: $!");
while (my $line = <DB>) {
chomp($line);
my @line = &split_decode($line);

next unless ($line[2] eq 'Yes'); ## Validate field
if ($line[$field_num] eq "$value") {
if ($line[7] ne "") { ## show only if there is a subtitle

$link .= qq|<A HREF="$db_script_link_url&Title=$line[6]&subtitle=$line[7]&ww=1&view_records=1">$line[7]</A><BR>|;
}
}
}
close (DB);

if ($link) {
return ('All Titles on this page:<P>' . $link . '<P>') };
}

Since some records have the same title but different subtitles, I modified the script to just display the subtitles in the list which is $line[7].

Any help would be greatly appreciated :)

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Help with small script - finding related records In reply to
Hi,

Have you tried:

Code:
$rec{'Title'} =~ s/&/&amp;/g;

..just before:

Code:
print &other_title_link("$rec{'Title'}", "6");

?

Had a few beers - but I think that should work :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Help with small script - finding related records In reply to
Yes, I have tried that ... but down with the sub. The call do the sub only passes the definition of the value and field number and then all the work is done in the sub itself.

print &other_title_link("$rec{'Title'}", "6");

Passing those values to this line within the sub:

if ($line[$field_num] eq "$value") {


I'm pretty sure whatever code to make this work needs to be where the database is being read and where the $link is defined.

Lois
Quote Reply
Re: [LoisC] Help with small script - finding related records In reply to
Hi,

Mmm.. you tried doing it via that line in other_title_link (wherever that is located).

I'm afraid I've not got a copy of DBMan - so can't really help in terms of where codes are/go etc - sorry Frown

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Help with small script - finding related records In reply to
Okay, I think I found the problem, since the value of the field is being pulled from within the record display the & and quotes are being converted before the value is passed to the sub. I need to convert those back using a conditional statement so it will match exactly what is actually in the database itself.

So the question now is how can I tell the script:

If $rec{'Title'} contains &amp;

OR

if $rec{'Title'} contains "

convert using:

$rec{'Title'} =~ s/\&amp; /\& /g;
$rec{'Title'} =~ s/\&quot;/\"/g;

and then print out the link:

print &other_title_link("$rec{'Title'}", "6");
}
else {
print &other_title_link("$rec{'Title'}", "6");
}

Hope that makes sense and somebody can help me with the syntax :)

Last edited by:

LoisC: Jul 21, 2008, 11:01 AM
Quote Reply
Re: [LoisC] Help with small script - finding related records In reply to
..again, not sure how its being called - but this should work for the conditions:

Code:
if ($rec{'Title'} =~ /\Q&amp;/ || $rec{'Title'} contains ") {
$rec{'Title'} =~ s/\&amp; /\& /g;
$rec{'Title'} =~ s/\&quot;/\"/g;
}

Hopefully that helps some :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Help with small script - finding related records In reply to
Thank you very very much Andy!!!


I added the code with my record display and changed it a bit .. and it all works perfectly now :)


if ($rec{'Title'} =~ /\Q&amp;/ || $rec{'Title'}=~ /\Q&quot;/ ) {
$rec{'Title'} =~ s/\&amp; /\& /g;
$rec{'Title'} =~ s/\&quot;/\"/g;
print &other_title_link("$rec{'Title'}", "6");
}
else {
print &other_title_link("$rec{'Title'}", "6");
}


Just changed:

if ($rec{'Title'} =~ /\Q&amp;/ || $rec{'Title'} contains ") {

to:

if ($rec{'Title'} =~ /\Q&amp;/ || $rec{'Title'}=~ /\Q&quot;/ ) {

I never could have figured this out myself. I really appreciate your help and knowledge :)

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Help with small script - finding related records In reply to
Hi,

No problem - glad to of been some help :)

Code:
if ($rec{'Title'} =~ /\Q&amp;/ || $rec{'Title'} contains ") {

haha whoops - didn't spot that boo boo :D

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [LoisC] Help with small script - finding related records In reply to
You may as well just use:

Code:
$rec{'Title'} =~ s/&amp;/&/g;
$rec{'Title'} =~ s/&quot;/"/g;

print &other_title_link("$rec{'Title'}", "6");

If it matches those strings it will replace them, otherwise it won't. There's not much point going to the extra effort of using an "if" block to check whether there's a match or not as the substitution basically does the same thing anyway.

Also note you don't need all the backslashes in the regexes. Only METACHARACTERS need to be escaped, eg. *, $, +, (, ), [, ]

Last edited by:

Wychwood: Jul 22, 2008, 2:35 AM