Jump to content

[SOLVED] Help with Text highlighting in search results


receiver

Recommended Posts

Please Help me with Text highlighting in search results:

The function should be
str_replace( $term, "<b>".$term."</b>", $search_results );

I need to enter the above code into the code source

the search type is like

(search engine gets db results, the type of source)
[code]$searchquery="select * from site where (name like'%$term%' or url like'%$term%' or `desc` like'%$term%') and credits > 0 and state = 'Enabled' order by credits DESC  limit $start,$resultperpage";

//echo $searchquery;
 
  $result=mysql_query($searchquery);

  if(mysql_num_rows($result)!=0){

  for($x=0;$x<mysql_numrows($result);$x++){

echo(" ".mysql_result($result,$x,'name')." <br> ".mysql_result($result,$x,'desc')."  ");
[/code]

I am not sure what to enter into the $search_results to work with the search engine script, maybe $searchquery but i tried.
($term is searched keyword)

can you help me

Thank You,

Thomas
I make it simpler.

the code has replace function(php)(should be right)

[code]tr_replace( $search_term, "<span>".$search_term."</span>", $search_results );[/code]

all $search_term should be replaced with <span>".$search_term."</span>

I entered my search engine script above in the first post just to you, that you can see what type of script the search engine is, not all the script just a part from the script.

I need the replace function into the script, but I need help with it, to put the replace function into the search engine script so that it will work also.

I am not 100% sure what i need to enter to "$search_term" that the function uses the search engine script right.

If you have any comments or more questions please post it, thank you for helping me.

Thomas

Here's a function I've used to make the search term bold in the results:

[code]
<?php
  function formatSearch($text,$term) {
    return preg_replace("/{$term}/i",'<b>${0}</b>',$text);  //note the ${0} in the replacement term keeps the original string that matched the search term
  }

?>
[code]

So, in your code, you might use it something like:

[code]
<?php
$searchquery="select * from site where (name like'%$term%' or url like'%$term%' or `desc` like'%$term%') and credits > 0 and state = 'Enabled' order by credits DESC  limit $start,$resultperpage";

//echo $searchquery;

  $result=mysql_query($searchquery);

  if(mysql_num_rows($result)!=0){
    while ($row = mysql_fetch_assoc($result)) {
      echo " ".formatSearch($row['name'],$term)." <br /> ".formatSearch($row['desc'],$term)."  ";
    }
  }

?>
[/code][/code][/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.