Re: [PHP] highlighting Search Results
If you want to highlight the EXACT search string, do a str_replace() on $row["Description"], $row["ProductName"], etc etc, replacing the search phrase with $searchSite. Pretty simple. $siteSearch", $row["Description"]); $productName = str_replace($siteSearch, "$siteSearch", $row["ProductName"]); ?> But, I'm guessing the search word is possibly more than one word??? If that's the case, you may wish to highlight each word individually: If that's the case, split the words in $searchSite into an array, and loop through through the array, replacing each word with word. Something like (untested): $word) { $row["Description"] = str_replace($word, "$word", $row["Description"]); $row["ProductTitle"] = str_replace($word, "$word", $row["ProductTitle"]); } echo $row["ProductTitle"].""; echo $row["Description"].""; ?> Although this will need further tweaking, because it's case sensitive (searching for "dog" and "Dog" will produce different results, it doesn't account for dog* (doggies, dogs, etc), and is perhaps a little limited in other ways, but should get you started). A well constructed reg exp instead of the str_replace will cover most of this, but i'm no good at them, and str_replace is quicker, IF it's all you need. Justin French Creative Director http://Indent.com.au on 06/05/02 9:26 PM, DC ([EMAIL PROTECTED]) wrote: > Hi all, > > I have a mySQL database running with PHP4. I have constructed a search form > ($searchSite) which returns results in a results page & now I want to have > the word that was entered in the search form highlight in the results page. > > eg User enters a search for products using the word " gardening" > > Results page might return: > > "this shovel is a great gardening tool" and "to be a expert in gardening, > you will need"...I want the word gardening to be bold wherever > it is displayed. > > My results page has the following code to display search results: BTW it > works like a dream...I just want to jazz it up a bit. > > > $result = mysql_query("SELECT * FROM products WHERE Description LIKE > '%$searchSite%' OR Keywords LIKE '%$searchSite%' OR CatName LIKE > '%$searchSite%' "); > > blah blah blah > > while ( $row = mysql_fetch_array($result) ) { > echo(""); > echo(" 'Arial,Helvetica,Sans serif'>" . $row["Description"] . ""); > echo(" 'Arial,Helvetica,Sans serif'>" . $row["ProductName"] . ""); > echo(" 'Arial,Helvetica,Sans serif'>\$" . $row["RetailPrice"] . ""); > > ?> > > How do I do this to highlight my search words? > > Thanks, any ideas much appreciated by this PHP newbie. > > DC > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] highlighting Search Results
Hi all, I have a mySQL database running with PHP4. I have constructed a search form ($searchSite) which returns results in a results page & now I want to have the word that was entered in the search form highlight in the results page. eg User enters a search for products using the word " gardening" Results page might return: "this shovel is a great gardening tool" and "to be a expert in gardening, you will need"...I want the word gardening to be bold wherever it is displayed. My results page has the following code to display search results: BTW it works like a dream...I just want to jazz it up a bit. "); echo("" . $row["Description"] . ""); echo("" . $row["ProductName"] . ""); echo("\$" . $row["RetailPrice"] . ""); ?> How do I do this to highlight my search words? Thanks, any ideas much appreciated by this PHP newbie. DC -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] highlighting search results (revisited)
Hi James I'll paste the code I use.. $string = search string entered in the text box. # // REPLACE SPACES FOR MULTIWORD SEARCH $string = str_replace(" ","%",$string); $db->query("SELECT title,description FROM files WHERE (title LIKE '%$string%') || (description LIKE '%$string%')"); // SPLIT THE SEARCH INTO PARTS FOR HIGHLIGHTING $bg = explode("%",$string); while($db->next_record()) { $title = $db->f("title"); $des = $db->f("description"); for($z=0; $z < count($bg); $z++) { // reset the array if it's at the end if(next($bg) == false) { reset($bg); } $bgstring = current($bg); $title = preg_replace("/($bgstring+)/ie", "'\\1'", $title); $des = preg_replace ("/($bgstring+)/ie", "'\\1'", $des); } echo $title.NL; echo $des.NL.NL; } ### The above code works as expectedexcept if I search for span and say class; now this will highlight span in the search string found from the database but on the second run ie. hightlighting class, it will re-highlight the in the span tags from the original pass. Do you get what I mean?? preg_replace is used to keep the original case of the results as it's a case insensitive search I need to be able to replace only result that aren't enclosed in a class tag Cheers M@ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
FW: RE: [PHP] highlighting search results (revisited)
> The input from the search form is $searchtext. The Returned > Search data is > $searchdata. > > > $sql = "SELECT searchdata FROM table WHERE searchdata LIKE > '%$searchtext'"; > $result = @mysql_query($sql, $connection) > or die (mysql_error()); > > $num = mysql_num_rows($result); > > $x = 0; > > if ($num == 0) { > echo "No results"; > } else { > > while($row = mysql_fetch_array($result)) { > $searchdata = $row['searchdata']; > > $x++; > > $searchdata = eregi_replace("$searchtext", " color=\"#FF\">$searchtext", $searchdata); > > echo "$x. $searchdata"; > > } > > } > > ?> > > Make sense? And does it help? Hi James Yes and no. I've got it to do that but what if the user enters more than one word? I need to be able to loop inside the while for each word to be able to highlight it. I think! The problem is if someone enters a word which is used within the hightlighting code. ie. font in your example. This would then hightlight this again and screw the resulting code. M@ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] highlighting search results (revisited)
I am trying to write a little search script to search a mysql db. I would like to highlight the search words for the displayed results, like sourceforge etc... After a little help, I have got it to hightlight when searching for one word. The problem is if I do a search for more than one word it repeats the hightlighting. here's the code I got so far $bg = explode("%",$string); while($db->next_record()) { $title = $db->f("title"); $des = $db->f("description"); for($z=0; $z < count($bg); $z++) { if(next($bg) == false) { reset($bg); } $bgstring = current($bg); $title = preg_replace("/($bgstring+)/ie", "'\\1'", $title); $des = preg_replace ("/($bgstring+)/ie", "'\\1'", $des); } echo $title.NL; echo $des.NL.NL; } The problem is is I search for class or span it screws up the code as it replaces the span or class in the original tag. How could I stop this? I know this another rex ex thing and I'm gonna get the oreilly book when I'm next in the city but untill then it's driving me bonkers. TIA M@ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]