This may sound dumb, i tried to highlight the searched letter/word in my result and found the below function, but not sure where should I put it, i have tried under tag and above tag inside php further below but no luck
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;
}
Fatal error: Call to undefined function sublinhamos() in /home/u472061620/public_html/search4.php on line 20 with the below code without inserting the code above.
<?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";
}
}
?>
Can anyone tell me where should I put the function sublinhamos in? tried within tag, above tag with/without tag, inside the below php tag...all no luck
<?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";
}
}
?>
don't worry about this, I have implemented truncate function as above thus don't seem necessary to highlight the searched keyword..however, if possible to implement this highlight function would be an extra feature. Thanks to those trying to help