0

如果每个用户都有 3 行,我需要知道如何进行实时搜索以查找完整名称……例如:

餐桌上的蔬菜

姓名、姓名、姓氏

Jhon-----erick--------doe

当我按照示例进行搜索时,jhon doe 或 erick doe 或 jhon erick doe 会向我展示结果

约翰·埃里克·多伊

这是我的实时搜索:

<?
$dbhost = "localhost";
$dbname = "";
$dbuser = "";
$dbpass = "";
global $db;
$db = new mysqli();
$db->connect($dbhost, $dbuser, $dbpass, $dbname);
$db->set_charset("utf8");
if ($db->connect_errno) {
    printf("Connect failed: %s\n", $db->connect_error);
    exit();
}
$html = '';
$html .= '<li class="result">';
$html .= '<a target="_self" href="urlString">';
$html .= '<h3>nombreString apellidoString de_casadaString</h3>';
$html .= '<h4>Expediente # : id_pacienteString</h4>';
$html .= '</a>';
$html .= '</li>';
$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search_string = $db->real_escape_string($search_string);
if (strlen($search_string) >= 1 && $search_string !== ' ') {
    $query = 'SELECT id_paciente,nombre,apellido,de_casada FROM PACIENTES WHERE id_paciente LIKE "%'.$search_string.'%" OR nombre LIKE "%'.$search_string.'%" OR apellido LIKE "%'.$search_string.'%" OR de_casada LIKE "%'.$search_string.'%"';
    $result = $db->query($query);
    while($results = $result->fetch_array()) {
        $result_array[] = $results;
    }
    if (isset($result_array)) {
        foreach ($result_array as $result) {
            $display_id_paciente = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['id_paciente']);
            $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['nombre']);
            $display_apell = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['apellido']);
            $display_casada = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['de_casada']);
            $display_url = 'paciente_personal_profile.php?id_paciente='.urlencode($result['id_paciente']);
            $output = str_replace('nombreString', $display_name, $html);
            $output = str_replace('apellidoString', $display_apell, $output);
            $output = str_replace('de_casadaString', $display_casada, $output);
            $output = str_replace('id_pacienteString', $display_id_paciente, $output);
            $output = str_replace('urlString', $display_url, $output);
            echo($output);
        }
    }else{
        $output = str_replace('urlString', 'javascript:void(0);', $html);
        $output = str_replace('nombreString apellidoString de_casadaString', '<b>Sin coincidencias.</b>', $output);
        $output = str_replace('id_pacienteString', 'No hay expediente', $output);
        echo($output);
    }
}
?>

在页面中显示结果:

<div id="main">
<h4 id="results-text">Mostrando resultados para: <b id="search-string">Array</b></h4>
<ul id="results"></ul>
</div>
4

1 回答 1

-1

您可以制作这样的视图:

CREATE VIEW MyView as 
SELECT id_paciente,nombre,apellido,de_casada, nombre+' '+apellido+' '+de_casada as complete_name FROM PACIENTES 

然后在视图中进行查询:

SELECT id_paciente,nombre,apellido,de_casada,complete_name FROM MyView
WHERE id_paciente LIKE "%'.$search_string.'%" OR complete_name LIKE "%'.$search_string.'%" OR nombre LIKE "%'.$search_string.'%" OR apellido LIKE "%'.$search_string.'%" OR de_casada LIKE "%'.$search_string.'%"
于 2013-06-28T10:23:04.903 回答