<?php

include('database_connection.php');

$column = array('slab_name', 'slab_number', 'slab_city', 'slab_mail','slab_nature_lab', 'slab_scope');

$query = "
SELECT * FROM tbl_slab 
";

if(isset($_POST['slab_nature_lab'], $_POST['slab_scope']) && $_POST['slab_nature_lab'] != '' && $_POST['slab_scope'] != '')
{
 $query .= '
 WHERE slab_nature_lab = "'.$_POST['slab_nature_lab'].'" AND slab_scope = "'.$_POST['slab_scope'].'" 
 ';
}

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

$query1 = '';

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

$statement = $connect->prepare($query);

$statement->execute();

$number_filter_row = $statement->rowCount();

$statement = $connect->prepare($query . $query1);

$statement->execute();

$result = $statement->fetchAll();



$data = array();

foreach($result as $row)
{
 $sub_array = array();
 $sub_array[] = $row['slab_name'];
 $sub_array[] = $row['slab_number'];
 $sub_array[] = $row['slab_city'];
 $sub_array[] = $row['slab_mail'];
 $sub_array[] = $row['slab_nature_lab'];
 $sub_array[] = $row['slab_scope'];
 $sub_array[] = '<a href="view.php?id='.$row['slab_id'].'" class="btn btn-primary"> View</a>';
 $data[] = $sub_array;
}

function count_all_data($connect)
{
 $query = "SELECT * FROM tbl_slab";
 $statement = $connect->prepare($query);
 $statement->execute();
 return $statement->rowCount();
}

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

echo json_encode($output);

?>
