Why are there duplicate rows in my table?
Why are there duplicate rows in my table?
Hello, I was trying to do the tutorial example here: https://datatables.net/examples/ajax/objects.html
I had to make some adjustments to the initialization call but I eventually got data to populate my table. However, there are duplicates of every row in the table. It looks like the call is run twice though in the network tab I only see one ajax call. I have verified my data and there are no duplicate rows when I run the query directly against the database and when I run the php file by itself.
HTML/JS:
<table id="training" class="display cell-border" style="width:98%;table-layout:fixed;">
<thead>
<tr>
<th>Employee</th>
<th>Function</th>
<th>Date</th>
<th>Score</th>
</tr>
</thead>
</table>
<script>
$(document).ready(function () {
$('#training').DataTable({
ajax: {
url: 'test.php',
dataSrc: ''
},
columns: [
{ data: 'Name'},
{ data: 'func'},
{ data: 'date'},
{ data: 'score'}
]
});
});
</script>
PHP:
try {
$conn = new PDO ($o,$user,$p);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOExeception $e){
echo "Connect Failed: " . $e->getMessage();
}
$qry = "select Employee.Name,Scores.func,Scores.date,Scores.score
from Scores
right join Employee on Scores.emp_id = Employee.ID
where Employee.Workgroup = 'Gen_Assy'
having Scores.score > 0";
$stmt = $conn->prepare($qry);
$stmt->execute();
$result = $stmt->fetchall(PDO::FETCH_ASSOC);
echo json_encode($result);
Answers
Are there duplicates in the JSON data when looking at the XHR response using the browser's network inspector?
I've never heard of or seen where Datatables duplicates the Ajax data. Can you post a link to your page so we can take a look?
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Or you can create a debugger log for the developers to look at.
Kevin
@kthorngren well I'm an idiot so just ignore me. I figured it out and it was user error. I'm new to this forum but I can't find a place to delete my question.