开发者

Php mysql search script, how to make single searched words bold if the search term wasn't the exact match to my database entry

Below is my script so far. This displays the users search term in bold if it is an exact match. For example. if the user searched "john bloggs" then it would be returned bold. whereas if the user searched for "john" then it would show the john bloggs entry but it wouldnt be in bold. Any help would be greatly appreciated.

<?
mysql开发者_运维问答_connect ("localhost", "user","pass")  or die (mysql_error());
mysql_select_db ("databasename");

$term = $_POST['term'];

$sql = mysql_query("select * from tablename where category like '%$term%' or title like '%$term%' or postcode like '%$term%' or info like '%$term%' ");

while ($row = mysql_fetch_array($sql)){

    echo '<br/> Category: '.str_replace($term,'<b>'.$term.'</b>',$row['category']);
    echo '<br/> Title:    '.str_replace($term,'<b>'.$term.'</b>',$row['title']);
    echo '<br/> Address:  '.$row['add1'];
    echo '<br/> Street:   '.$row['street'];
    echo '<br/> City:     '.$row['city'];
    echo '<br/> Postcode: '.str_replace($term,'<b>'.$term.'</b>',$row['postcode']);
    echo '<br/> Phone:    '.$row['phone'];
    echo '<br/> E-Mail:   '.$row['email'];
    echo '<br/> Website:  '.$row['website'];
    echo '<br/><br/>';

}
?>


I'm using the following:

/**
 * higlights search string with HTML5-mark
 *
 * @param string needle search string
 * @param string haystack original text, may contain search string
 * @return string original text with additional HTML-mark highlighting
 */
function highlight($needle, $haystack)
{
    return preg_replace('/(' . preg_quote($needle, '/') . ')/i', '<mark>$1</mark>', $haystack);
}

Usage:

echo '<br/> Category: ' . highlight($term, $row['category']);


I guess the problem that you are facing is the %term% thing. On the database it will look up for any matched whatever the position the term is. So if you try to do a string replace "foo bar" it will search only for the matching string "foo bar" and not "foo" or "bar".

You can do the following while ($row = mysql_fetch_array($sql)){ $terms = explode(" ", $term); foreach ($terms as $wordToReplace) { $category = str_replace($wordToReplace,'<b>'.$wordToReplace.'</b>',$row['category']); $title = str_replace($wordToReplace,'<b>'.$wordToReplace.'</b>',$row['title']); $postCode = str_replace($wordToReplace,'<b>'.$wordToReplace.'</b>',$row['postcode']); } // and then show it echo '
Category: '.$category; echo '
Title: '.$title; echo '
Address: '.$row['add1']; echo '
Street: '.$row['street']; echo '
City: '.$row['city']; echo '
Postcode: '.$postCode; echo '
Phone: '.$row['phone']; echo '
E-Mail: '.$row['email']; echo '
Website: '.$row['website']; }

NOTE:This code was not tested!


If you're just looking for something quick and functional what you have should be fine. If you wanted to improve it a bit you may want to use styles instead of the <b> tag and use PHP's strcasecmp() function

For exmaple:

$field = (strcasecmp($term, $row['foo'])==0) ? "<span class='bold'>".$row['foo']."</span>" : $row['foo'];


Try this:

echo '<br/> Category: '.($term == row['category'] ? '<b>'.$term.'</b>' : $row['category']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜