Jquery Autocomplete

Hi,

Good day!

I created a search box and my problem is when I type Employee ID the auto list displayed is Employee Name, I need to display on the list is based on what I type. I don’t know if it is possible that in one search box I can search Employee name then the list of names will display, when I type Employee ID, employee id list will display. same with Passport no and res id.

Now, when I type Employee Id or Passport No or res id and employee name. the displayed list is employee name.

this is my code:

<script type="text/javascript" src="js/jquery.js"></script>
     <script type='text/javascript' src='js/jquery.autocomplete.js'></script>
     <link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />
<script type="text/javascript">
//----auto complete emp no--//

$().ready(function() {
  $("#search_data").autocomplete("get_emp_info.php", {
  width: 237,
    minLength: 3,//search after three characters
  matchContains: true,
  mustMatch: true,
  selectFirst: false
  });
   
  });
</script>

  <table>
     <tr>
     <td style="border: none;color:#80600a;font-weight:bold;">Search:</td>
     <td><input type="text" name="search_data" id="search_data" value="" size="35" autofocus></td>
     </tr>
     </table>
<?php
ob_start();
include('includes/connection.php');
$q = strtolower($_GET["q"]);

if ($q == '') {
  header("HTTP/1.0 404 Not Found", true, 404);

}
//else (!$q) return;
else{
$sql = "SELECT pe.employee_no, pe.employee_name, pe.passport_no,
   gov.res_id
   FROM tbl_personal_info AS pe JOIN tbl_public_info AS pu ON (pe.employee_no = pu.employee_no) JOIN tbl_e_government_info AS gov ON (pu.employee_no = gov.employee_no)
   WHERE pe.employee_no LIKE '%".$q."%' OR pe.employee_name LIKE '%".$q."%' OR pe.passport_no LIKE '%".$q."%' OR gov.res_id LIKE '%".$q."%'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
     $pid = $row["employee_no"];
     $employee_name = $row["employee_name"];
     $passport_no = $row["passport_no"];
     $res_id = $row["res_id"];
 
     
     echo "$employee_name|$pid|$passport_no|$res_id\n";
   }
} else {
  echo "0 results";
}
$conn->close();  
}
?>

sample:

employee_no = BN262
employee_name = Anna
passport_no = 12345
res_id = 54321

when i type BN the list display should be all employee_no with BN but in my code it display the list of employee name with BN employee_no.

Thank you

You can check what your $q is containing and build SQL query depending on that

For example:

if (preg_match('/^BN([0-9]*)$/i', $q){

    // q starts with bn and numbers
    // this is an employee_no
    $sql = "SELECT ... WHERE pe.employee_no LIKE '{$q}%'";

} else if (is_numeric($q)) {

    // q contains only numbers
    // this is either passport_no or res_id
    $sql = "SELECT ... WHERE pe.passport_no LIKE '%{$q}%' OR gov.res_id LIKE '%{$q}%'";

} else {

    // none of above
    // so maybe this is a name
    $sql = "SELECT ... WHERE pe.employee_name LIKE '%{$q}%'";

}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.