-1

I am using this code to highlight search keywords:

<form method="post">  
    <input type="text" name="keyword" value="<?php if(isset($_GET["keyword"])) echo $_GET["keyword"]; ?>" /> 
    <input type="submit" name="submit" value="Search" />  
</form>  

<?php 
if(isset($_GET["keyword"]))  
{  
    $condition = '';  
    $query = explode(" ", $_GET["keyword"]);  
    foreach($query as $text)  {  $condition .= "name LIKE '%".mysqli_real_escape_string($conn, $text)."%' OR "; }  
    $condition = substr($condition, 0, -4);  
    $sql_query = "SELECT * FROM products WHERE " . $condition;  
    $result = mysqli_query($conn, $sql_query);  
    if(mysqli_num_rows($result) > 0)  
    {  
        while($row = mysqli_fetch_array($result))  
        {  
            echo ''.$row["name"].'';  
        }  
    }  
    else  {  echo '<label>Data not Found</label>';  }  
}  
?>

However, this highlights only one "keyword".

If the user enters more than one keyword, it will narrow down the search but no word is highlighted. How can I highlight more than one word?

N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
behroz
  • 84
  • 7
  • 2
    I don't see any highlighting in your posted script. Appending empty strings to both sides of a variable is ...weird/unnecessary ...who is teaching people to code like this? – mickmackusa Sep 09 '19 at 08:23
  • If you mean "how to search for multiple keywords?" There are probably tens of duplicates for you to find on Stack Overflow. – mickmackusa Sep 09 '19 at 08:28
  • 1
    Possible duplicate of [php mysqli prepared statement LIKE](https://stackoverflow.com/questions/18527659/php-mysqli-prepared-statement-like) – Dharman Sep 09 '19 at 08:36
  • I want to bold the keyword loop, iphone apple S7 – behroz Sep 09 '19 at 08:38
  • example : http://s2.picofile.com/file/8371867292/oo.jpg – behroz Sep 09 '19 at 08:46

1 Answers1

0

For multiple search with like should use regexp

For example

SELECT * from products where name REGEXP 'apple|table'; 

dılo sürücü
  • 3,821
  • 1
  • 26
  • 28
  • I want every word found in the search results That bold word If the word is found, it will be bold in the search results http://s3.picofile.com/file/8371886668/shot.jpg – behroz Sep 09 '19 at 13:44
  • Which of the following code can make the word bold? `$highlighted = preg_replace("|($text)|Ui" , "$1" , $row['name'] );` OR `$highlighted = preg_filter('/' . preg_quote($_GET['keyword'], '/') . '/i', '$0', $row['name']);` – behroz Sep 09 '19 at 13:52
  • You can make filter with Vuejs or angular.js repeat data and for filter create make custome filter ... I u want just php ,Not in this way.you must retrieve two array and merge all product and filter array ,in filler array have include input values – dılo sürücü Sep 09 '19 at 13:57
  • I want to highlight the search results with a few keywords found – behroz Sep 09 '19 at 14:02
  • I think You can do with js.i think no with PHP .search word in Dom element if found change html of Dom element.all filters generally make with js – dılo sürücü Sep 09 '19 at 14:11
  • I want to bold the word with this code, example: http://s5.picofile.com/file/8371911526/bold.jpg – behroz Sep 09 '19 at 20:59