Advertisement
mubateknologi

mahasiswa-serverside.php

Sep 6th, 2022 (edited)
2,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2. include 'config/database.php';
  3.  
  4. if ($_GET['action'] == "table_data") {
  5.  
  6.     $columns = array(
  7.         0 => 'id_mahasiswa',
  8.         1 => 'nama',
  9.         2 => 'prodi',
  10.         3 => 'jk',
  11.         4 => 'telepon',
  12.         5 => 'id_mahasiswa'
  13.     );
  14.  
  15.     $querycount = $db->query("SELECT count(id_mahasiswa) as jumlah FROM mahasiswa");
  16.     $datacount = $querycount->fetch_array();
  17.  
  18.     $totalData = $datacount['jumlah'];
  19.  
  20.     $totalFiltered = $totalData;
  21.  
  22.     $limit = $_POST['length'];
  23.     $start = $_POST['start'];
  24.     $order = $columns[$_POST['order']['0']['column']];
  25.     $dir = $_POST['order']['0']['dir'];
  26.  
  27.     if (empty($_POST['search']['value'])) {
  28.         $query = $db->query("SELECT id_mahasiswa,nama,prodi,jk,telepon FROM mahasiswa ORDER BY $order $dir LIMIT $limit OFFSET $start");
  29.  
  30.     } else {
  31.         $search = $_POST['search']['value'];
  32.         $query = $db->query("SELECT id_mahasiswa,nama,prodi,jk,telepon FROM mahasiswa WHERE nama LIKE '%$search%' OR telepon LIKE '%$search%' ORDER BY $order $dir LIMIT $limit OFFSET $start");
  33.  
  34.         $querycount = $db->query("SELECT count(id_mahasiswa) as jumlah FROM mahasiswa WHERE nama LIKE '%$search%' OR telepon LIKE '%$search%'");
  35.  
  36.         $datacount = $querycount->fetch_array();
  37.         $totalFiltered = $datacount['jumlah'];
  38.     }
  39.  
  40.     $data = array();
  41.     if (!empty($query)) {
  42.         $no = $start + 1;
  43.         while ($value = $query->fetch_array()) {
  44.             $nestedData['no'] = $no;
  45.             $nestedData['nama'] = $value['nama'];
  46.             $nestedData['prodi'] = $value['prodi'];
  47.             $nestedData['jk'] = $value['jk'];
  48.             $nestedData['telepon'] = $value['telepon'];
  49.             $nestedData['aksi'] = '';
  50.             $data[] = $nestedData;
  51.             $no++;
  52.         }
  53.     }
  54.  
  55.     $json_data = [
  56.         "draw"            => intval($_POST['draw']),
  57.         "recordsTotal"    => intval($totalData),
  58.         "recordsFiltered" => intval($totalFiltered),
  59.         "data"            => $data
  60.     ];
  61.  
  62.     echo json_encode($json_data);
  63. }
  64. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement