<?php
include 'config.php';
 
// Number of records fetch
$numberofrecords = 10;
 
if(!isset($_POST['searchTerm'])){
 
    // Fetch records
    $stmt = $conn->prepare("SELECT * FROM pegawai ORDER BY namapegawai limit :limit");
    $stmt->bindValue(':limit', (int)$numberofrecords, PDO::PARAM_INT);
    $stmt->execute();
    $usersList = $stmt->fetchAll();
 
}else{
    echo "xxxxx";
    $search = $_POST['searchTerm'];// Search text
    // Fetch records
  //$stmt = $conn->prepare("SELECT * FROM pegawai WHERE namapegawai like :name ORDER BY namapegawai LIMIT :limit");
  $stmt = $conn->prepare("SELECT * FROM pegawai WHERE namapegawai like :namapegawai ORDER BY namapegawai limit :limit");
    $stmt->bindValue(':namapegawai', '%'.$search.'%', PDO::PARAM_STR);
    $stmt->bindValue(':limit', (int)$numberofrecords, PDO::PARAM_INT);
    $stmt->execute();
    $usersList = $stmt->fetchAll();
 
}
     
$response = array();
 
// Read Data
foreach($usersList as $user){
    $response[] = array(
        "nip" => $user['nip'], //for value
        "text" => $user['namapegawai'],
      "html" =>  "<div style='color:black'>".$user['nip']."</div><div><small><i>".$user['namapegawai']."</i></small></div>",
        "title" =>  $user['namapegawai']
    );
}
 
echo json_encode($response);
exit();