Forums

PHP

This topic is locked

Do I need to use a session variable?

Posted 30 Aug 2006 12:21:21
1
has voted
30 Aug 2006 12:21:21 Iain MacDonald posted:
Hi there,

At the moment I'm just about there with some search functionality, searching mainly on a lengthy CV type field. For this reason, the field itself only appears on the details page, not the results page.

I need the search terms to highlight in the CV field, and have this working fine, passing it through as part of the URL variable :

<pre id=code><font face=courier size=2 id=code>&lt;a href="candidateDetails3.php?CandidateID=&lt;?php echo $row_Recordset1['CandidateID']; ?&gt;&keyword=&lt;?php echo ($_POST['keyword']); ?&gt;"&gt;&lt;?php echo $row_Recordset1['FirstName']; ?&gt; &lt;?php echo $row_Recordset1['LastName']; ?&gt;&lt;/a&gt;</font id=code></pre id=code>

This works fine with search strings not in quotes, but not with strings with quotes (that are used to search for phrases, rather than words).

If you search on :

proven track record

to search for any of the three words, the end of the link is correct as :

...keyword=proven track record"

but if you search on :

"proven track record"

to find instances of the exact phrase, then the end of the link is given as :

...keyword=\"

And the code at the top of the page that is sorting out the search terms in quotes looks like :

<pre id=code><font face=courier size=2 id=code>&lt;?php function highlightResult($keyword,$result) {
if (get_magic_quotes_gpc()) {
$replace = stripslashes($keyword);
}
else {
$replace = $keyword;
}
$replace = preg_replace("/\"|\,/i","",$replace);
$replace = preg_replace("/ {2,}/i"," ",$replace);
$replaceArr = explode(" ",$replace);
for ( $i = 0; $i &lt; sizeof($replaceArr); $i++ ) {
$result = preg_replace("/{$replaceArr[$i]}/i","&lt;b&gt;{$replaceArr[$i]}&lt;/b&gt;",$result);
}
$highlighted = $result;
return $highlighted;
}
?&gt; </font id=code></pre id=code>

So basically I was wondering if I need to use a session variable here to pass it through? I haven't really used these before tho', so am unsure as to how to go about it.

If anyone could point me in the right direction, or post some sample code for how to capture it in the first place on the results page, and then grab it again so that the phrases are highlighted on the details page, that would be great.

Cheers.

Replies

Replied 30 Aug 2006 13:30:28
30 Aug 2006 13:30:28 Roddy Dairion replied:
instead of using $_POST i would recoment that you use $_GET in your form.
Replied 31 Aug 2006 10:32:27
31 Aug 2006 10:32:27 Iain MacDonald replied:
OK - I've changed that.

Also tried the session variable route - it's fine for highlighting the search string, again as long as it's not in quotes.

But any search string that is in quotes, eg "a phrase like this", then it doesn't get highlighted whan passed to the details page, whereas if the search term is a regular series of words not in quotes to create a phrase, then it's fine - so it looks as tho' the whole search string in quotes might solve the issue of being able to search on multiple phrase, it causes this new problem.

Any ideas or suggestions?
Replied 31 Aug 2006 11:35:38
31 Aug 2006 11:35:38 Roddy Dairion replied:
so what you're saying is with quotes it will look for phrases without it will look for each word. but when i test the script it works fine. i search for 2 phrases, they both became bold and one word which also became bold like this <b>welcome to my</b> word, <b>how</b> are you <b>am fine thanks</b>.. I don't understand what your problem is?
Replied 31 Aug 2006 15:01:54
31 Aug 2006 15:01:54 Iain MacDonald replied:
The problem is that if I search on, say, "proven track record", it appears highlighted correctly in the results page, but not once passed through to a details page.

Or at least I'm sure it wasn't last night - looking at it here on a PC rather than a Mac (although that shouldn't make any difference?), the 'track is highlighting on the results page, but proven and record are not.

Which was the original problem with my highlighting code that now accounts for the "" - so I'll check later that that code is also in the details page.

But certainly something funny was going on.

And even then, it's not quite right, as even tho' the search is only returning instances where the exact phrases are matched, all instances of each word are being highlighted, which issn't ideal.

I'll double check the code and what it's doing and post again later.

Reply to this topic