doudun1029 2013-12-05 03:56
浏览 26
已采纳

too long

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

  • 写回答

1条回答 默认 最新

  • dongshi8038 2013-12-05 04:19
    关注

    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. 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100