0

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

Philip Tiong
  • 113
  • 2
  • 10
  • Have you tried [this jquery plugin](http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html)? – Martin Aug 12 '13 at 13:58
  • 1
    Your code is insecure. It has sql injection and xsrf bugs. Read about security first. – Marek Aug 12 '13 at 14:00
  • You are not displaying a string in your result, instead a newline separated values (fields) for which the highlighter function helps a little. You can simply format the product_name with a tag. No need to search and replace. Or be specific where you want the highlight. – Bere Aug 12 '13 at 14:11

1 Answers1

0

First of all you didn't select details from DB (you only selected id and product_name). So you can't access it like $details= $row["details"];

I recommend:

Update the SQL like

$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);

Now every searched word in product name and detail will appear highlighter.

Warning! Your code is open to sql injection attack! sanitize the $_POST values first!

Bere
  • 1,627
  • 2
  • 16
  • 22
  • i have added preg replace, does that prevent sql injection? also, I put in the sublinhamos but it shows undefined function. where should I put the function that define it? – Philip Tiong Aug 13 '13 at 00:01
  • @PhilipTiong see http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Bere Aug 13 '13 at 06:12