PHP Associative Array (Server Side Processing)

PHP Associative Array (Server Side Processing)

sitawsitaw Posts: 2Questions: 1Answers: 0

Just want to know what is the best approach when processing associative array with DataTables?

The system flow is:
1. My web app request to a third-party API
2. API will return array like the following:

[
'code'              => 0,
'message'           => 'OK',
'visitor_list'      => [
    [
        'firstname'     => 'Gilbert',
        'lastname'      => 'Arenas',
        'visit_date'    => '2020-01-01'
    ],
    [
        'firstname'     => 'Chris',
        'lastname'      => 'Paul',
        'visit_date'    => '2020-01-02'
    ],
    [
        'firstname'     => 'Jerry',
        'lastname'      => 'Sloan',
        'visit_date'    => '2020-01-03'
    ]
]

then I will process the visitor_list array on my controller before I display it on DataTables in view.

What approach should I do when displaying thousands of record from my to controller to DataTables? I was confused because on my other projects, I just use the DataTables server side processing (filter/where clause, order by, limit, offset, other mysql queries, etc.) but with this current project Im doing, I do not have access to the backend other than an API request which will return multiple (thousands) rows.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin
    Answer ✓

    Hi,

    If you have less than 50k rows, you should be fine with client-side processing. Load the data using ajax (assuming the API can return JSON) and ajax.dataSrc set to visitor_list to tell it to read the array of rows from that property rather than the default of data.

    Also then use columns.data to tell DataTable which property to use from the row objects for each column.

    If you can't modify the API and it only offers the option to get all of the data in a single shot, there isn't much you can do about that.

    Allan

  • sitawsitaw Posts: 2Questions: 1Answers: 0

    Thanks a lot, Allan!

This discussion has been closed.