2

I have a problem with which I am unsure how to deal with:

I have recovered a big JSON from an API, whose contents I need to show in my website (all ok here), but I need to return the current data shown in the website (it can be deleted, edited and modified before being sent back through my AJAX call), so that I can add that data to another website, also through an API.

I can recover and add without problems with my current logic, the problem comes when I get a big JSON from my AJAX call:

$.post('ajax/send_data.php?command=create&id=' + id, {data: superArray}, function(response){
    $('#data_table').html(response);
});

Even if I send my data like this, the max. capacity is exceeded, which results in the creation of around 30 rows of data. Changing the max_input_vars in php.ini isn't an option, I can't change those myself, plus it should be set to a really big number to fit, or have no limit...

The only solution that comes to my mind (after several others that aren't applicable to my situation) is to loop over the "array", sending the data in chunks with multiple $.post calls...

The problem is that my "array" is not an array, as php sends back an object, being it originally an associative array.

The question is... how can I loop over my object, divide into chunks, and send back to PHP through AJAX those chunks?

Here's what I was trying, using as reference Convert and Loop through JSON with PHP and JavaScript Arrays/Objects

for (var key in superArray) {
    str += JSON.stringify(superArray[key]);
    cnt++;
    if(cnt % 25 == 0){
        console.log(str);
        temparray = JSON.parse(str); // here gives an error
        $.post('ajax/send_data.php?command=create&id=' + id, {data: temparray}, function(response){
            str = "";
        });
    }
}

I can't turn back my string chunks into an object, as the key is lost in the process (Each data is created like:

undefined{ "id":"1338", "name":"24HRS ", "description":"..." })

What am I doing wrong here? is there maybe a better approach to my problem that I am maybe not aware of?

Edit:

That's what I send back from my Ajax call:

echo json_encode($html);
Myriam S.
  • 21
  • 3
  • Only thing that comes to mind is `.splice`. Have a look [here](http://stackoverflow.com/questions/7273668/how-to-split-a-long-array-into-smaller-arrays-with-javascript) – Andrei Aug 31 '15 at 09:48
  • Can you try str += JSON.stringify({key: superArray[key]}); ? Just a wild guess. – Lajos Arpad Aug 31 '15 at 09:58
  • the problem, as I said, is that it is not an array. It is an object containing other objects... I tried to use slice, but didn't work, even trying to do var j = superArray.lengh; gives undefined – Myriam S. Aug 31 '15 at 10:06
  • Lajos Arpad, it gives back "undefined{"key":{"id":"1338","name":"24HRS","description":""..."} – Myriam S. Aug 31 '15 at 10:11
  • I have written a converter function. I do not fully understand your problem. Hopefully my answer will be helpful for you. – Lajos Arpad Aug 31 '15 at 10:13
  • Another possible solution could be sending your json compressed to your server. I have not try that, but it looks promising to me: https://github.com/tcorral/JSONC – skroczek Aug 31 '15 at 10:18
  • When you say `my array is not an array as php sends back an object` you could typecast it back to an associative array using `$myObject = (array)$myObject;` and then continue piping chunks to your controller to update your view. – Halfpint Aug 31 '15 at 10:27
  • @MyriamS., by the way, the problem is caused by the fact that you are passing an empty object, like this: {data: temparray}. I am sure you wanted to do something like this: $.post('ajax/send_data.php?command=create&id=' + id, data: {row: temparray}, – Lajos Arpad Aug 31 '15 at 10:32
  • No Lajos Arpad, I use {data: temparray} because that's the name I want to give to the variable that will be passed to php through AJAX, and works fine in the first example I give, just that I can't send all the data I want because of the size (I get an "Input variables exceeded 1000" warning) – Myriam S. Aug 31 '15 at 10:46
  • You are right, it seems I am too tired :) – Lajos Arpad Aug 31 '15 at 10:48
  • skroczek it sounds promising, though the data amount I need to send can be 100 times of the actual data I am being able to send (I send 30 when needing 3000 sometimes). I am not sure if this size if I am mixing concepts here, what I get is an "Unknown: Input variables exceeded 1000" warning – Myriam S. Aug 31 '15 at 10:50
  • sorry Alex, I think I explained myself bad, PHP sends to JQuery an object when making an "echo json_encode($response);", I tried doing as you say "echo (array)json_encode($response);", but I receive the same on client's side – Myriam S. Aug 31 '15 at 11:02

0 Answers0