#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Amsterdam, NL
    Posts
    383
    Rep Power
    17

    Question Highlight search result from mysql database search


    Hi,

    I've found this piece of code which will highlight the keywords in the result from a search.

    PHP Code:
    function highlight_keywords($string$keywords) {
        if (
    $keywords != "" || $keywords != NULL) {
            
    $words explode(" "$keywords);
            foreach (
    $words as $word) {
                
    $position 0;
                while (
    $position !== false) {
                    
    $position strpos(strtolower($string), strtolower($word), $position);
                    if (
    $position !== false) {
                        
    $replace_string substr($string$positionstrlen($word));
                        
    $replace_string "<span class=\"keyword\">" $replace_string "</span>";
                        
    $string substr_replace($string$replace_string$positionstrlen($word));
                        
    $position $position strlen($replace_string);
                    }
                } 
            }
        }
        return 
    $string;

    However i have difficulty implementing this bit of code, because I dont know how to "transfer" the $keyword to the actual page that is displayed. My search works like this.
    1. Someone enters a keyword in a search form, and presses the search button.
    2. The mysql queries searches the database tables for records that contain the keyword.
    3. A list of pages is displayed that contain the records in which the keyword was found.
    4. By clicking the link to the page, the actual page containing the keyword is shown.

    Now I want to highlight the keywords in that page. My problem is I don't know how to let php "remember" the keywords and replace it when someone click the link in the results page to show the page containing the result.
    I think I will include the function in a page with other functions, that will be "required" by the page containing the end result, then... ehhh.... I'm lost.

    This is probably a noob question , but any help is greatly appreciated.

    Cheers,
    Gurt
    If "Thank you" is too much effort to type, then a simple "ty" will do as well!
    Please post back your solution, even if you found it yourself! Don't let others munch on your problem while it's not your problem anymore.
    OK....I haven't actually RTFM, but more sort of 'looked into' TFM. Does it show? My pet-project: www.divestart.com
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    14
    Rep Power
    0
    You could use $_GET[]: file.php?highlight="word".

    Alternatively, you could use sessions/cookies to remember the word.

    Hope that helps.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Amsterdam, NL
    Posts
    383
    Rep Power
    17
    Hi delusions,

    I am gonna find out if that helps
    If you or anybody else would be able to give me some more hints in teh right direction, that would be great!

    Cheers,
    Gurt
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Amsterdam, NL
    Posts
    383
    Rep Power
    17
    It works like a champ!

    I included the function in a page that holds more functions, and that is called when a function is called.

    All I had to do is somehow transfer the assigned value of $keywords from the list page to the details page of my site. I did this by including at the end of the link of the results the value:
    &keywords=$keywords

    so a link like:
    www.whatever.com/module/function=detail&bla=$bla

    should become:
    www.whatever.com/module/function=detail&bla=$bla&keywords=$keywords

    Now I had to enclose all the functions that display my information with the function tag. If I had a result from the database displaying a description of something, like:
    $description.

    it would become:
    highlight_keywords($description, $keywords)

    Hope this helps other people as well.
    Cheers,
    Gurt
    If "Thank you" is too much effort to type, then a simple "ty" will do as well!
    Please post back your solution, even if you found it yourself! Don't let others munch on your problem while it's not your problem anymore.
    OK....I haven't actually RTFM, but more sort of 'looked into' TFM. Does it show? My pet-project: www.divestart.com

IMN logo majestic logo threadwatch logo seochat tools logo