0

I have "conditional content" base on the type of content, like so:

//--- Here goes a nice SQL where I get the content, this works ok. ---
if(mysqli_num_rows($niceSQL) > 0) {
    while($something = mysqli_fetch_array($niceSQL)) {

        $type = $something["type"];

        if ("type" == "A") {
            $data1 = $something["data1"];
            $data2 = $something["data2"];
            $data3 = $something["data3"];

            $something_A = array(
                "one" => $data1,
                "two" => $data2,
                "three" => $data3,
            );

            echo json_encode($something_A, JSON_FORCE_OBJECT);
        } else if ("type" == "B") {
            $data1 = $something["data1"];
            $data2 = $something["data2"];
            $data3 = $something["data3"];

            $something_B = array(
                "one" => $data1,
                "two" => $data2,
                "three" => $data3,
            );

            echo json_encode($something_B, JSON_FORCE_OBJECT);
        }
    }
}

I'm trying to dump every array as JSON for handle it with jQuery Each.

The problem is all that gets "printed" (echoed) at same time, so the AJAX response is "parsererror". I get this error in this way:

(ajax and config here).fail(function( jqXHR, textStatus ) {
    console.log( "Request failed: " + textStatus );
})

So, how can I print every $something_A or $something_B independently but together? Together means only one ajax request, so I can handle the content with jQuery Each.

If I print only the first $something_A or the first $something_B then I can do the "each" loop (that's why I assume the PHP "json_encode" function is working properly), but it's not useful to me (because I'm receiving only the 1st of many "somethings").

Note: I can change the logic of the dumping, not how I get the content (SQL + while).

Jimmy Adaro
  • 1,325
  • 15
  • 26
  • Gather all values in an array and encode it. – u_mulder Jan 02 '17 at 19:45
  • This results in an Array with _n_ Arrays inside. – Jimmy Adaro Jan 02 '17 at 19:53
  • Yes it is, and what do you need? – u_mulder Jan 02 '17 at 19:54
  • I need _n_ Arrays for handle them with jQuery each. I've dump all in JS console and I get something like this: array with 2 values, array with 3 values, array with 4 values.. all that inside of one Array (`$master_dump_array`) where I do `$master_dump_array[] = $something_X;` – Jimmy Adaro Jan 02 '17 at 20:00
  • n arrays __must__ be grouped somehow. Otherwise you simply __cannot__ work with them. In javascript array can be iterated with a simple `for` loop. – u_mulder Jan 02 '17 at 20:02
  • How can I get inside of the `$master_dump_array` for access to those _n_ arrays? Anyways, I'm getting 1 master which contains 2, 3 or 4 arrays (looks like somehow it's incrementing itself), and then in every of those arrays I get the data I need. – Jimmy Adaro Jan 02 '17 at 20:05
  • This can be found in a 5-sec googling, but I'll save your time http://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript – u_mulder Jan 02 '17 at 20:09

3 Answers3

1

At the beginning, create a new array: $result = []; Then in the while loop, change echo json_encode($something_A, JSON_FORCE_OBJECT); to $result[] = $something_A; After the loop, use: echo json_encode($result, JSON_FORCE_OBJECT);

Jirka Hrazdil
  • 3,983
  • 1
  • 14
  • 17
  • Yes, I've already tried that. The only issue I find is now I'm getting all the results inside of other array. So the jQuery Each function can't work properly. – Jimmy Adaro Jan 02 '17 at 19:52
1

you can only send one json output per ajax request

create an outer master array that has 2 properties 'A' and 'B' that are each empty arrays then push each row to appropriate one, and after loop echo the master array

$output= array(
   'A'=>array(),
   'B'=>array()
);

while($something = mysqli_fetch_array($niceSQL)) {

    $type = $something["type"];

    if ("type" == "A") {
       .......
       $output['A'][] = $something_A;

    } else if ("type" == "B") {
       ......
       $output['B'][] = $something_B;
    }
}

echo json_encode($output);

Then in jQuery you have a single object with 2 properties that are each an array

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • No luck with this. The array is pushing new data but also adding older elements. – Jimmy Adaro Jan 02 '17 at 22:49
  • what do you mean by `older elements`? Sounds like you are fighting 2 different problems. – charlietfl Jan 02 '17 at 22:52
  • Yes, totally looks like that. Using Chrome, if I move to "Network tab > load.php" (the file with the SQL and Arrays), I see the JSON output. When I check that JSON in http://jsonlint.com/ it shows me this error: http://i.imgur.com/ldkSzBB.png That's what I'm trying to solve. That thing goes over and over again. – Jimmy Adaro Jan 02 '17 at 23:01
  • 1
    That isn't the structure my answer would create. You are still echoing inside a loop. See first statement in my answer...can only output json once per request – charlietfl Jan 02 '17 at 23:05
  • I don't know why is doing that structure then. I have something like this: `if(mysqli_num_rows($query) > 0)`, `while($data = mysqli_fetch_array($query))`, `if(mysqli_num_rows($query) > 0) {`, `while($something = mysqli_fetch_array($query)) {` – Jimmy Adaro Jan 02 '17 at 23:07
0

What about putting the echo at the end of the script?

  1. Replace your echo json_encode($something_A, JSON_FORCE_OBJECT); and echo json_encode($something_B, JSON_FORCE_OBJECT); with array_push($mySolutionArray, $something_).
  2. At the end of the script place this: echo $mySolutionArray.

Don't forget to initialize $mySolutionArray

Iván Rodríguez Torres
  • 4,293
  • 3
  • 31
  • 47