Browse Source

function to highlight search terms in search.php

main
Simon Bowie 2 years ago
parent
commit
bdc1f1546a
2 changed files with 20 additions and 0 deletions
  1. +11
    -0
      site/public/search.php
  2. +9
    -0
      site/text_functions.php

+ 11
- 0
site/public/search.php View File

@@ -8,6 +8,7 @@
# @purpose: Searches the Solr index

include '../solr.php';
include '../text_functions.php';

?>

@@ -33,6 +34,7 @@ include '../solr.php';

foreach($search_results as $result):

$result = highlight_search_terms($result, $_POST["search"]);
?>
Application ID:

@@ -115,5 +117,14 @@ include '../solr.php';
?>

</div>
<script>
let text = "<?php echo $_POST["search"]; ?>";
upperText = text.toUpperCase();
result = "<span style='color:green'>" + upperText + "</span>";
const collection = document.getElementsByClassName("search_term");
for (let i = 0; i < collection.length; i++) {
collection[i].innerHTML = result;
}
</script>
</body>
</html>

+ 9
- 0
site/text_functions.php View File

@@ -0,0 +1,9 @@
<?php

function highlight_search_terms($array, $search_term){
$replacement = "<span class='search_term'>$search_term</span>";
$result = str_ireplace($search_term, $replacement, $array);
return $result;
}

?>

Loading…
Cancel
Save