<?php
//fetch.php
$connect = mysqli_connect("localhost", "root", "", "olahdata");
$columns = array('kode_klasifikasi','perihal','uraian','kurun_waktu');

$query = "SELECT * FROM tatausaha ";

if(isset($_POST["search"]["value"]))
{
 $query .= '
 WHERE perihal LIKE "%'.$_POST["search"]["value"].'%" 
 OR uraian LIKE "%'.$_POST["search"]["value"].'%" 
 ';
}

if(isset($_POST["order"]))
{
 $query .= 'ORDER BY '.$columns[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' 
 ';
}
else
{
 $query .= 'ORDER BY no_urut DESC ';
}

$query1 = '';

if($_POST["length"] != -1)
{
 $query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}

$number_filter_row = mysqli_num_rows(mysqli_query($connect, $query));

$result = mysqli_query($connect, $query . $query1);

$data = array();

while($row = mysqli_fetch_array($result))
{
 $sub_array = array();
 $sub_array[] = '<div contenteditable class="update" data-id="'.$row["no_urut"].'" data-column="kode_klasifikasi">' . $row["kode_klasifikasi"] . '</div>';
 $sub_array[] = $row["perihal"];
 $sub_array[] = $row["uraian"];
 $sub_array[] = $row["kurun_waktu"];
 $sub_array[] = '<button type="button" name="detail" id="detail" data-id="'.$row["no_urut"].'" class="btn btn-success btn-xs">Detail</button>';
 $data[] = $sub_array;
}

function get_all_data($connect)
{
 $query = "SELECT * FROM tatausaha";
 $result = mysqli_query($connect, $query);
 return mysqli_num_rows($result);
}

$output = array(
 "draw"    => intval($_POST["draw"]),
 "recordsTotal"  =>  get_all_data($connect),
 "recordsFiltered" => $number_filter_row,
 "data"    => $data
);

echo json_encode($output);

?>