I am writing a PHP application that returns info to a datatable with ajax and serverside being the option. I am running into a few issues mainly with searching and pagination. Currently, neither of these works although enabled and I have tried to make my own search query on the PHP side. Just getting an invalid JSON response for this. Pagination I am not sure about if I have to implement anything special for but I assume not. Pagination on my datatable currently displays that there are multiple pages but when selecting them it doesn't cycle through the data for the second page instead of still showing the data for the first, this happens on every page when selected.
My Datatable looks like this http://i.edbrook.site/brave_YhbmzijyBF.png http://i.edbrook.site/brave_kzlVgX4TnP.png
My code for the JS looks like this (for some context I am getting a value from the URL to parse through the ajax, I know this is insecure that's another thing I will do at a later time)
$(document).ready(function(){
// Gets All Parameters from the URL
let searchParams = new URLSearchParams(window.location.search)
// Tries to See if the Player Paramter is in the URL if it isnt throw a Error.
let param = searchParams.has('player') // true
if (!param) {
console.error("No Player Key Entered ")
}
// If a Parameter does exsist.
else {
// Get Statistical Data
console.log("Executed");
// Data Table Gets information for all Events.
var EventsData = $('#eventList').DataTable({
"lengthChange": false,
"processing": true,
"serverSide": true,
"order": [],
"ajax": {
url: "action.php",
type: "POST",
data: {action: 'listevents', id: searchParams.get('player')},
dataType: "json"
},
"language": {
"lengthMenu": "_MENU_",
"search": ""
},
"columnDefs": [
{
"targets": [0, 1, 2, 3],
"orderable": true,
},
],
"pageLength": 25
});
console.log(EventsData)
}
});
For the PHP
public function getPlayerEvents($id) {
$PlayerListErrorMessage = '';
$sqlQuery = "SELECT * FROM Edbtvplays_UnturnedLog_Events WHERE PlayerId = '".$_POST["id"]."' ";
// If there is a search request.
if(!empty($_POST["search"]["value"])){
$sqlQuery .= '(EventType = "'.$_POST["search"]["value"].'" ';
$sqlQuery .= ' OR EventData LIKE "%'.$_POST["search"]["value"].'%" ';
}
if(!empty($_POST["order"])){
$sqlQuery .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
} else {
$sqlQuery .= 'ORDER BY EventType DESC ';
}
$result = mysqli_query($this->dbConnect, $sqlQuery);
$filterednumRows = mysqli_num_rows($result);
// Gets the total number of rows in the database for this user as its used for datatables list.
$totalquery = "SELECT * FROM Edbtvplays_UnturnedLog_Events WHERE PlayerId = ".$id.";" ;
$numRows = mysqli_num_rows($result);
$EventData = array();
while($Event = mysqli_fetch_assoc($result) ) {
$EventRows = array();
$EventRows[] = $Event['EventType'];
$EventRows[] = $Event['EventData'];
$EventRows[] = $Event['ServerId'];
$EventRows[] = $Event['EventTime'];
$EventData[] = $EventRows;
}
// Inner join the server to get the Server name to display on the Event table.
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $numRows,
"recordsFiltered" => $filterednumRows,
"data" => $EventData
);
echo json_encode($output);
}
For Actipn.php which calls the PHP above ( i have a lot of tables and wanted to make it neat in classes)
else if(!empty($_POST['action']) && $_POST['action'] == 'listevents') {
$players->getPlayerEvents();
}
Any help would be appreciated, thanks.