如何突出搜索结果中已用于搜索的关键字?

时间:2013-08-12 13:53:05

标签: php search highlight

这可能听起来很愚蠢,我试图在我的结果中突出显示搜索到的字母/单词并找到了下面的功能,但不知道我应该把它放在哪里,我已经尝试过下面的标签和上面的标签,但是没有运气

   function sublinhamos($text,$searchquery) {
        $wordsArray = array();
        $markedWords = array();
        // explode the phrase in words
        $wordsArray = explode(' ', $searchquery); 




        foreach ($wordsArray as $k => $searchquery) {
          $markedWords[$k]='<mark>'.$searchquery.'</mark>';
        }




        $text = str_ireplace($wordsArray, $markedWords, $text);




        //right trows results
        return $text;
    }

致命错误:使用以下代码调用第20行/home/u472061620/public_html/search4.php中的未定义函数sublinhamos(),而不插入上述代码。

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');


$search_output = "";

if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
        $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
    if($_POST['filter1'] == "Whole Site"){
    $sqlCommand = "(SELECT * FROM products WHERE product_name LIKE '%$searchquery%' OR details LIKE '%$searchquery%') ";
    } 
    require_once("storescripts/connect_to_mysqli.php");
    $query = mysqli_query($myConnection,$sqlCommand) or die(mysqli_error($myConnection));
    $count = mysqli_num_rows($query);
    if($count >= 1){
        $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
        while($row = mysqli_fetch_array($query)){
                $id=$row["id"];
            $product_name = sublinhamos($row["product_name"],$searchquery);
                    $details = sublinhamos($row['details'],$searchquery); 
                $category=$row["category"];
                $subcategory=$row["subcategory"];
            $search_output .= "ID: $id <br/> Name: $product_name -<br/>$details<br />$category<br/>$subcategory<br/>
<a href='product.php?id=$id'>link</a><br/>";



        } // close while
    } else {
        $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
    }
}

?>

谁能告诉我应该在哪里放入功能sublinhamos?尝试在标签内,带有/不带标签的标签上方,在下面的php标签内...都没有运气

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.

function myTruncate($string, $limit, $break=" ", $pad="...")
{
  // return with no change if string is shorter than $limit
  if(strlen($string) <= $limit) return $string;

  // is $break present between $limit and the end of the string?
  if(false !== ($breakpoint = strpos($string, $break, $limit))) {
    if($breakpoint < strlen($string) - 1) {
      $string = substr($string, 0, $breakpoint) . $pad;
    }
  }

  return $string;
}

$search_output = "";

if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
        $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
    if($_POST['filter1'] == "Whole Site"){
    $sqlCommand = "(SELECT * FROM products WHERE product_name LIKE '%$searchquery%' OR details LIKE '%$searchquery%') ";
    } 
    require_once("storescripts/connect_to_mysqli.php");
    $query = mysqli_query($myConnection,$sqlCommand) or die(mysqli_error($myConnection));
    $count = mysqli_num_rows($query);
    if($count >= 1){
        $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
        while($row = mysqli_fetch_array($query)){
                $id=$row["id"];
            $product_name =$row["product_name"];
                    $details = $row["details"]; 
                $category=$row["category"];
                $subcategory=$row["subcategory"];
            $description = "<a href='product.php?id=$id'>ID: $id <br/> Name: $product_name -<br/>$details<br />$category<br/>$subcategory<br/>
link</a><br/>";
$search_output = myTruncate($description, 100," ");
        } // close while
    } else {
        $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
    }
}

?>

不要担心这个,我已经实现了如上所述的truncate函数,因此似乎没有必要突出显示搜索的关键字。但是,如果可能的话,实现这个突出显示功能将是一个额外的功能。感谢那些试图提供帮助的人

1 个答案:

答案 0 :(得分:0)

首先,您没有从DB中选择details(您只选择了id和product_name)。因此,您无法像$details= $row["details"];

那样访问它

我建议:

一样更新SQL

$query = "Select * FROM products WHERE product_name LIKE '%$searchquery%' OR details LIKE '%$searchquery%')"; // every column fetched

...

$details = sublinhamos($row['details'],$searchquery); $product_name = sublinhamos($row['product_name'],$searchquery);

现在,产品名称和细节中的每个搜索字词都会显示为荧光笔。

警告!你的代码是开放的SQL注入攻击!首先清理$ _POST值!