此脚本可以搜索,但我希望它突出显示搜索到的部分。
实施例: 输入:'Hel'
输出1:'大家好!'
输出2:'我的生活就像地狱'
带输出的页面不需要从hello和地狱(不同的字体颜色或背景颜色)突出显示部分'hel'
这是我的代码:
<?php
mysql_connect("xxxxxx","xxx","xxxxx") or die("Kan geen verbinding maken met de server!");
mysql_select_db("xxxx") or die("Kan de ingevoerde database niet vinden");
$query = $_GET['query'];
$min_length = 0;
if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM product
WHERE (UPPER(naam) LIKE UPPER('%".$query."%'))
OR (UPPER(titel) LIKE UPPER('%".$query."%'))
OR (UPPER(druk) LIKE UPPER('%".$query."%'))")
or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){
echo "<table >";
echo"<th>Auteur</th> <th>Titel</th> <th>Druk</th>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr><td>" .
'<a href="bio.php?naam='.$results["naam"].'">' . $results["naam"] . '</a><br>' .
"</td> <td>" .
'<a href="article.php?id='.$results["id"].'">' . $results["titel"] . '</a><br>' .
"</td><td>" .
$results['druk'].
"</td></tr>";
}
echo "<table>";
}
else{
echo "<div style=\"text-align:center; padding-bottom: 10px; \"><br /><br />Geen resultaten, probeer het opnieuw of <a href=index.php>ga terug</a></div>";
}
}
else{
echo "Minimum length is ".$min_length;
}
?>
有人可以帮助我并告诉我接下来该做什么吗?
答案 0 :(得分:0)
首先将你的代码压缩
如果您的代码更正,只需在while循环中使用一个简单的函数,例如
$results["naam"] = str_replace($_GET['query'],$results["naam"],'<span class="highlight">'.$results["naam"].'</span>';
答案 1 :(得分:0)
喜欢这个http://php.net/manual/de/function.str-replace.php
试
$highlightPart = str_replace($query, '<span class="myHighlightClass">' . $query . '</span>', $results["naam"]);
在您的脚本中:
<?php
mysql_connect('xxxxxx','xxx', 'xxxxx') or die('Kan geen verbinding maken met de server!');
mysql_select_db('xxxx') or die('Kan de ingevoerde database niet vinden');
$query = $_GET['query'];
$min_length = 0;
if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query('SELECT * FROM product
WHERE (UPPER(naam) LIKE UPPER(\'%' . $query . '%\'))
OR (UPPER(titel) LIKE UPPER(\'%' . $query . '%\'))
OR (UPPER(druk) LIKE UPPER(\'%' . $query . '%\'))')
or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){
echo '<table>';
echo '<th>Auteur</th> <th>Titel</th> <th>Druk</th>';
while($results = mysql_fetch_array($raw_results)){
$highlightNaam = str_replace($query, '<span class="myHighlightClass">' . $query . '</span>', $results["naam"]);
$highlightTitel = str_replace($query, '<span class="myHighlightClass">' . $query . '</span>', $results["titel"]);
echo "<tr><td>" .
'<a href="bio.php?naam=' . $results['naam'] . '">' . $highlightNaam . '</a><br>' .
'</td> <td>' .
'<a href="article.php?id=' . $results['id'] . '">' . $highlightTitel . '</a><br>' .
'</td><td>' .
$results['druk'].
'</td></tr>';
}
echo '<table>';
}
else{
echo '<div style=\"text-align:center; padding-bottom: 10px; \"><br /><br />Geen resultaten, probeer het opnieuw of <a href=index.php>ga terug</a></div>';
}
}
else{
echo 'Minimum length is ' . $min_length;
}
?>
答案 2 :(得分:0)
您可以使用函数str_replace()
$search_str=$_GET['query'];
$replace_str='<font color="#value">'. $search_str.'</font>'; //you can change any style you like
$outdata=str_replace($search_str,$replace_str,$results["titel"]);
echo "<tr><td>" .
'<a href="bio.php?naam='.$results["naam"].'">' .
$results["naam"] .
'</a><br>' .
"</td> <td>" .
'<a href="article.php?id='.$results["id"].'">' .
$outdata . //the outdata with your style
'</a><br>' .
"</td><td>" .
$results['druk'].
"</td></tr>";