gpt4 book ai didi

php - 搜索结果中的粗体搜索词

转载 作者:行者123 更新时间:2023-11-28 23:40:21 25 4
gpt4 key购买 nike

当用户搜索我的数据库时,我希望搜索词在结果中以粗体显示。我似乎找不到可以轻松实现到我现有代码中的教程或解释。我是 PHP 新手,所以请耐心等待。所以,我想要的是,如果有人搜索“blue”,那么结果会显示类似这样的内容:

搜索词:
蓝色

结果:
1 - 蓝色 - 马 - 黑色体色 - 红色眼睛
2 - 吉米 - 马 - 蓝色 体色 - 黑眼睛

等等等等。

这是我的 search.php 页面代码:

<?php
$query = $_GET['query'];
// gets value sent over search form

$min_length = 3;
// you can set minimum length of the query if you want

if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to &gt;

$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection

$raw_results = mysql_query("SELECT * FROM characters
WHERE (`name` LIKE '%".$query."%') OR (`player` LIKE '%".$query."%') OR (`dam` LIKE '%".$query."%') OR (`sire` LIKE '%".$query."%') OR (`status` LIKE '%".$query."%')") or die(mysql_error());

// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table

// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

echo "Searched term:<br>";
echo "<b>" . $query . "</b>";
echo "<br><br>";
echo "Results:<br>";
foreach($results as $index => $resultArray) {
$resultString = $index+1 . " - ";
foreach($resultArray as $key => $value) {
if (strpos($value, $query) !== FALSE) { // strpos returns a value between 0 and n if the string is found; if it's NOT found, it returns FALSE - due to PHP's veeeerry loose typing, we need to use !== rather than simply !=, because otherwise 0 will return **as** FALSE
$resultString .= "<b>" . $value . "</b>";
} else {
$resultString .= $value;
}
$resultString .= " - ";
}
echo substr($resultString, 0, -3) . "<br>"; // We're chopping off the last " - "
}
}

}
else{ // if there is no matching rows do following
echo "No results found.";
}

}
else{ // if query length is less than minimum
echo "Search term is invalid. Minumum search length is: ".$min_length;
}
?>

预先感谢您的帮助。

在使用以下建议之一后,我收到大量错误:警告:为第 63 行的/home3/hunstami/public_html/characters/search.php 中的 foreach() 提供的参数无效1

我猜我只是试图将其错误地实现到我的代码中,但正如我上面所说,我对此完全陌生。

最佳答案

在我的脑海中,有两种方法可以做到这一点。两者都涉及将您计划写入屏幕的 html 保存在一个变量中一段时间​​。

方法 A - 在遍历结果集时将字符串加粗

// In while loop
row = "#".$results['id']." - ";

if $results['name'] == $query {
row = row."<strong>".$query."</strong>";
else {
row = row.$query;
}

row = row." - ".$results['breed']." - ".$results['gender'] // SNIP, use the rest like you use it now

方法 B - 返回一个字符串并对搜索词进行查找和替换

html = "";

// while loop
html = html."#".$results['id']." - ".$results['name']." - ".$results['breed']." - ".$results['gender']." - ".$results['sire']." x ".$results['dam']." - ".$results['genetics']." - ".$results['body']." Base - ".$results['mane']." Mane - ".$results['tail']." Tail - ".$results['eye']." Eyes - ".$results['markings']." - Born: ".$results['birthdate']." - ".$results['bodytype']." Body Type - ".$results['traits']." - ".$results['defects']." - ".$results['extras']." - Achievements: ".$results['achievements']." - Status: ".$results['status']." - Notes: ".$results['notes']." - Played by ".$results['player']."<br><br>";

// end while

html = str_replace(html, $query, "<strong>".$query."</strong>");

echo html;

关于php - 搜索结果中的粗体搜索词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34573607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com