自动完成加入2列MySQL& PHP

时间:2015-06-04 09:49:56

标签: php mysql autocomplete

我有一个自动填充表单,从表中拉出第一个名字。但是我想使用2个字段自动完成。这两个字段是NAME和LAST_NAME,它们是数据库中的2个单独字段。这是我的搜索代码:

<?php
include('db_connection.php');
if(isset($_POST['search_keyword']))
{
    $search_keyword = $dbConnection->real_escape_string($_POST['search_keyword']);
    $sqlUsers="SELECT ID, NAME, LAST_NAME FROM b_user WHERE NAME LIKE '%$search_keyword%'";
    $resUsers=$dbConnection->query($sqlUsers);

    if($resUsers === false) {
        trigger_error('Error: ' . $dbConnection->error, E_USER_ERROR);
    }else{
        $rows_returned = $resUsers->num_rows;
    }

$bold_search_keyword = '<strong>'.$search_keyword.'</strong>';
if($rows_returned > 0){
        while($rowUsers = $resUsers->fetch_assoc()) 
        { 
            echo '<div class="show" align="left"><span class="user_name">'.str_ireplace($search_keyword,$bold_search_keyword,$rowUsers['NAME']).'</span></div>'; 
        }
    }else{
        echo '<div class="show" align="left">No matching records.</div>'; 
    }
} 
?>

以上代码会在下拉列表中返回第一个名称,但我需要它来搜索NAME,然后继续访问LAST_NAME。有没有办法加入NAME和LAST_NAME。我试过这个:

 $sqlUsers="SELECT ID, CONCAT(NAME, ' ', LAST_NAME) AS FULLNAME FROM b_user WHERE FULLNAME LIKE '%$search_keyword%'";

然后显示:

 <span class="user_name">'.str_ireplace($search_keyword,$bold_search_keyword,$rowUsers['FULLNAME']).'</span>

但这似乎彻底打破了搜索。我觉得我与上述并不遥远,但还没有快乐。感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以在查询中将NAME和LAST_NAME分开,这样可以更好地控制如何输出自动填充的结果。您只需要更改查询的WHERE子句以查找姓氏上的匹配项。我认为自动完成通常非常松散匹配,这可能会有效。

13.804 2 1 0.128 14.10683054308584
13.805 4 1 0.1 16.404063320609485
13.845 4 1 0.1 18.88784810910845
13.887 3 0 0.5 15.885403793537332
13.900 2 1 0.128 17.432392753144814
13.910 0 1 0.15 14.065390777016884
13.919 2 1 0.128 14.303367876393343
13.925 0 1 0.15 99.709674389069306
13.967 4 0 0.1 16.166182416686354
13.987 2 0 0.128 27.678527587660298
13.990 0 0 0.15 16.335622854953012
13.991 3 0 0.5 18.135064819504496
14.017 2 0 0.128 32.702333086991857
14.034 1 0 0.25 23.747057691617819
14.042 1 0 0.25 21.28645813221631
14.042 4 0 0.1 32.412778602456548
14.042 4 0 0.1 16.355373059379346
14.049 4 0 0.1 16.9107377441049
14.061 4 0 0.1 14.247305256107564
14.068 3 0 0.5 17.503919428057454
14.085 0 0 0.15 15.027892690096117
14.098 0 0 0.15 18.872227052735838
14.098 1 0 0.25 32.769497723363287
14.100 0 0 0.15 14.169509642396147
14.123 0 0 0.15 15.384714430934654
14.127 0 0 0.15 16.186777071833166
14.159 2 0 0.128 26.62600293363089
14.161 0 0 0.15 14.378881668687308
14.176 2 1 0.128 32.252258371764931
14.206 4 0 0.1 15.524216799990542
14.255 4 0 0.1 22.657759416211846
14.277 4 0 0.1 15.098140555235378
14.309 1 1 0.25 14.907511075479166
14.338 2 1 0.128 23.05330560466021
14.340 0 1 0.15 19.689321884956239
14.356 3 0 0.5 134.27562471483711
14.360 0 1 0.15 16.688768682324934
14.371 4 1 0.1 15.574474425386159
14.372 4 1 0.1 18.3483146897773
14.380 0 1 0.15 22.180348478940314
14.396 0 1 0.15 17.998478450267356
14.402 2 1 0.128 15.971205833886451
14.405 2 1 0.128 28.328755195061586
14.457 2 1 0.128 75.285563263357076
14.521 2 1 0.128 23.344965171769058
14.528 4 1 0.1 16.572185579506026
14.534 0 1 0.15 55.568075650030558