I was trying to figure out how to sort an array in jQuery. Previously, I tried the code in PHP and it works well but this time I want to load the data using jQuery. This is what I have:
fetch.js
<script>
$(document).ready(function(){
$.get("fetch.php")
.done(function (data) {
var displayString = "";
var record = [];
$.each(JSON.parse(data), function (key, value) {
if(!array_key_exists(value['ACC'], record))
{
record[value['ACC']][value['ACC']]['ACC'] = value['ACC'];
record[value['ACC']][value['ACC']]['CATEGORY'] = value['CATEGORY'];
record[value['ACC']][value['ACC']]['CVALUE'] = value['CVALUE'];
record[value['ACC']][value['ACC']]['FACILITY'] = value['FACILITY'];
record[value['ACC']][value['ACC']]['FUND'] = value['FUND'];
record[value['ACC']][value['ACC']]['AMOUNT'] = value['AMOUNT'];
}
else
{
record[value['ACC']][value['ACC']]['CATEGORY'] = value['CATEGORY'];
record[value['ACC']][value['ACC']]['CVALUE'] = value['CVALUE'];
}
});
ksort(record, SORT_NUMERIC);
$.each(JSON.parse(record), function (key, value) {
displayString +=
"<tr>" +
"<td>" + value[key]['ACC'] + "</td>" +
"<td>" + value[key]['CATEGORY'] + "</td>" +
"<td>" + value[key]['CVALUE'] + "</td>" +
"<td>" + value[key]['FACILITY'] + "</td>" +
"<td>" + value[key]['FUND'] + "</td>" +
"<td>" + value[key]['AMOUNT'] + "</td>" +
"</tr>";
});
$("#tableBody").html(displayString);
$("#table-display").dataTable({
dom: 'Blfrtip',
scrollX: true
});
});
});
</script>
And here's my php file to fetch the data
fetch.php
<?php
require 'connection.php';
$query = "select * from table";
$result = oci_parse($conn, $query);
oci_execute($result);
$arr = array();
while ($row = oci_fetch_array($result)){
$arr[] = $row;
}
echo json_encode($arr);
?>
I'm expecting the output will be like image below where it group into single value for repeatable value in column table.
Image output is using php code.
My goal is to display it using jQuery script above.
Appreaciate if someone can help me on this. Kindly help and guide me. Thanks in advance.