0

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.

enter image description here

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.

Rzj Hayabusa
  • 504
  • 6
  • 20
  • 1
    Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – VLAZ Nov 12 '19 at 08:56
  • @VLAZ . No. I want to do it using jQuery – Rzj Hayabusa Nov 12 '19 at 09:03
  • 2
    Why in jQuery specifically? JavaScript is perfectly capable of [finding a key](https://stackoverflow.com/questions/1098040/checking-if-a-key-exists-in-a-javascript-object) and [sorting](https://stackoverflow.com/questions/1494713/how-does-javascripts-sort-work) without jQuery. – VLAZ Nov 12 '19 at 09:08
  • 1
    You have to (slightly) rethink the concept or data-structure here, because JS does not have associative Arrays. It has Objects, but it [does not guarantee order of the keys](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) so a function like `ksort` would not make any sense in JS. `array_key_exists` would be as simple as `if(keyName in object)` – Thomas Nov 12 '19 at 09:21
  • 1
    @Rozaimi What have you tried so far? – Andrew Nov 12 '19 at 09:21

0 Answers0