0

I have an array from php and here it is

$results = array(
        "sEcho" => 1,
        "iTotalRecords" => count($newdata),
        "iTotalDisplayRecords" => count($newdata),
        "aaData" => $newdata
);

echo json_encode($results, JSON_PRETTY_PRINT); 

all of the data is also packed as array inside aaData my question here is how can I get that data? my target is to populate that in a table and here is my code.

<script>
        var i,x;
        var tb = $("#col_details");

        $(document).ready(function() {
            $.ajax({
                url: 'qry_collection.php',
                type: "POST",
                datatype: 'json',
                success: function(data) {
                /* Delete table content */
                data = JSON.parse(data);
                console.log(data);
                $("#col_details tr").remove();
                /* Populate the table */
                   for (i = 0; i < data.length; i++) {
                       var tr = $("<tr />");
                       for (x in data[i]) {
                            var td = $("<td />");
                            td.html(data[i][x]);
                            tr.append(td);
                            count = i 
                        }
                        tb.append(tr);
                    }
                }
            });
        });
    </script>

I can see the array using the console but populating it as table is my prob.

TYSM

here is the image from console

enter image description here

14
  • You can't 'get' a PHP variable from AJAX; you'd need to build the table whilst passing in the data on the PHP side. Commented Nov 3, 2017 at 2:27
  • what do you mean? create the table in the php already? I think I can get the data because it shows on console. Commented Nov 3, 2017 at 2:30
  • If you add a breakpoint inside the array or object loop, can you step through and see if it's constructing the cells and rows as expected? Commented Nov 3, 2017 at 2:32
  • 1
    What I mean is that you can't access the $results variable from an AJAX POST. You'd need to echo out $results on qry_collection.php. Commented Nov 3, 2017 at 2:35
  • 1
    Something like : for (var i=0; i<data.aaData.length; i++) { console.log(data.aaData[i]); } Commented Nov 3, 2017 at 2:59

1 Answer 1

2

data is an Object with properties of sEcho, iTotalRecords, iTotalDisplayRecords, aaData. You want to iterate over data.aaData which an array of whatever $newdata is.


EDIT:

success: function(data) {
    data = JSON.parse(data);
    /* Delete table content */
    $("#col_details tr").remove();
    /* Populate the table */
    $("#col_details").html(data.reduce((out, tr) => `${out}<tr>${d.reduce((o, td) => `<td>${td}</td>`, '')}</tr>`, ''));
}

@Aloso pointed out that this is likely to be run straight in a browser, so your probably don't want to use template strings.

In that case you switch up the formatting a little:

function(data) {
    data = JSON.parse(data);
    /* Delete table content */
    $("#col_details tr").remove();
    /* Populate the table */
    $("#col_details").html(data.reduce(function(out, tr) { return out + '<tr>' + d.reduce(function(o, td) { return '<td>' + td + '</td>'}, '')} + '</tr>'}, ''));
}
Sign up to request clarification or add additional context in comments.

8 Comments

yes sir is it possible to get it over and put it inside a table?
and by the the $newdata is an output of a query
@NardongBagsik try updating your outer loop to iterate over data.aaData, not data because it's an object. Aside: not every community member here is a "sir".
Yes. I don't know what each of your elements is, but what you have there will at least sort of work, you are just iterating over the wrong thing in your outer loop. for (i = 0; i < data.length; i++) { should be for (i = 0; i < data.aaData.length; i++) {
@stealththeninja noted and sorry for the wrong call
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.