【问题标题】:Live search function, how to make result to the input box实时搜索功能,如何在输入框中生成结果
【发布时间】:2013-12-05 03:56:51
【问题描述】:
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);
}
}

这是一个实时搜索功能。搜索电子邮件地址,并显示匹配的名字和姓氏。我有两个问题。

1:当我输入@时它不会给我任何结果。

2:我希望我可以点击结果并输入输入框,但现在它只是重新打开页面。

3:我只想搜索@之前的邮件地址。比如 text@hotmail.com。我只需要搜索文本而不是 hotmail.com。我怎么能做到这一点。电子邮件在数据库中

【问题讨论】:

  • 您在寻找 jqueryUI 自动完成库吗?做一些研究。
  • 我输入@时没有结果,你知道为什么吗?

标签: php database search livesearch


【解决方案1】:

在输入框输入@时这部分的问题

    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() 如果主题参数是一个数组,则返回一个数组,否则返回一个字符串。

看看preg_replace()函数

 If matches are found, the new subject will be returned, otherwise subject  
 will be returned unchanged or NULL if an error occurred. 

【讨论】:

  • 当然名字里没有@,因为我正在搜索邮件,并显示该邮件的所有者姓名
  • @user3066938 好的,那你在这里做什么? $display_function = preg_replace("/".$search_string."/i", "&lt;b class='highlight'&gt;".$search_string."&lt;/b&gt;", $result['firstName']);
猜你喜欢
  • 1970-01-01
  • 2019-12-25
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多