Question

if (strlen($search_string) >= 1 && $search_string !== ' ') {
// Build Query
$query = 'SELECT * FROM User WHERE email LIKE "%'.$search_string.'%"';

// Do Search
$result = $tutorial_db->query($query);
while($results = $result->fetch_array()) {
    $result_array[] = $results;
}

// Check If We Have Results
if (isset($result_array)) {
    foreach ($result_array as $result) {

        // Format Output Strings And Hightlight Matches
        $display_function = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['firstName']);
        $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['lastName']);
        //$display_url = 'http://php.net/manual-lookup.php?pattern='.urlencode($result['function']).'&lang=en';

        // Insert Name
        $output = str_replace('nameString', $display_name, $html);

        // Insert Function
        $output = str_replace('functionString', $display_function, $output);

        // Insert URL
        //$output = str_replace('urlString', $display_url, $output);

        // Output
        echo($output);
    }
}else{

    // Format No Results Output
    $output = str_replace('urlString', 'javascript:void(0);', $html);
    $output = str_replace('nameString', '<b>No Results Found.</b>', $output);
    $output = str_replace('functionString', 'Sorry :(', $output);

    // Output
    echo($output);
}
}

This is a live search function. search email address, and display matched first name and last name. I have two problems.

1: It will give me no result when I type @.

2: I wish I can click the result and put in the input box, but right now it does nothing but reopen the page.

3: I only want to search the email address before @. like text@hotmail.com. I only need to search text but not hotmail.com. How am I able to do this. emails are in database

Was it helpful?

Solution

Problem in this part when you type @ in the input box

    foreach ($result_array as $result) {

    // Make Sure you have @ in your $result['firstName']
    $display_function = preg_replace( 
                               "/".$search_string."/i", 
                               "<b class='highlight'>".$search_string."</b>", 
                               $result['firstName']);

    // Make Sure you have @ in your $result['lastName']
    $display_name = preg_replace(
                               "/".$search_string."/i", 
                               "<b class='highlight'>".$search_string."</b>", 
                               $result['lastName']);



// Insert Name
$output = str_replace('nameString', $display_name, $html);

// Insert Function
$output = str_replace('functionString', $display_function, $output);

// Insert URL
        //$output = str_replace('urlString', $display_url, $output);

        // Output
        echo($output);
    }

preg_replace() returns an array if the subject parameter is an array, or a string otherwise.

Take a look on preg_replace() function

 If matches are found, the new subject will be returned, otherwise subject  
 will be returned unchanged or NULL if an error occurred. 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top