Forum Moderators: coopster & phranque

Message Too Old, No Replies

append keyword to script ONCE

         

GlassEye

10:20 pm on Apr 21, 2005 (gmt 0)

10+ Year Member



please help,

I got my typical substandard script modification done the other day, simple job prepending a term to a search term for a specialty search. However as the script pages through the results it adds the term multiple times. so the first page is 'blue widgets' the second page is for 'blue blue widgets'

here's the place where the prepend is done..

$keywords = $cgi->param('keywords');
if( $keywords ne "" ) {
$keywords = "LOOPS" . " " . $keywords;
}
(where LOOPS is the keyword that keeps showing up)
is there a quick fix to this block that will make it so that if the term LOOPS has already been added to the $keyword, it won't add it again?

If you take pity on me this time, I promise not to buy bad programming in the future. I don't really wnat to go back to the moron who fixed it, just to have him try to charge me for the 'extra work.'

GlassEye

4:31 am on Apr 22, 2005 (gmt 0)

10+ Year Member



should I do it by modifying/removing the condition statement? for instance I don't care if the keywords variable is non-empty or not, that's inconsequential compared to problem.

But I would rather include a logical AND to the if statement that means AND "LOOPS" is not already present in the keyword parameter. How would I do that?

GlassEye

2:08 pm on Apr 22, 2005 (gmt 0)

10+ Year Member



I tried this

$keywords = $cgi->param('keywords');
if( $keywords!= /LOOPS/) {
$keywords = "LOOPS" . " " . $keywords;
}

but that doesn't actually append the keyword. I thought this meant

if the keyword parameter doesn't contain a substring matching LOOPS

then add LOOPS to the string

It does solve the problem of the next result page, if I type in a search like 'LOOPS widgets'. But if I type in 'widgets' as I intend for users to do, it doesn't append the extra term. Please help.

rocknbil

3:20 pm on Apr 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried this
$keywords = $cgi->param('keywords');
if( $keywords!= /LOOPS/) {
$keywords = "LOOPS" . " " . $keywords;
}

You almost had it. You want a pattern match:

$keywords = $cgi->param('keywords');
if( $keywords!~ /LOOPS\s+/i) {
$keywords = "LOOPS" . " " . $keywords;
}

Not the change of the = to a tilde ~. The i makes it case insensitive, take it or leave it. The \s+ looks for a space following LOOPS, which should prevent it from matching on "LOOPS" in some other part of $keywords.

GlassEye

8:23 pm on Apr 22, 2005 (gmt 0)

10+ Year Member



Thanks Rocknbil,

That works perfectly, and puts my mind at ease.

Joe