How do I get repetitive data with jquery ajax dataType json

Hello,
I’m getting data with json using ajax to parse the data, no problem here
There is a page function where I get data with ajax

page_ajax.php

function goster($variable){
        $jsonData = array(
            "index1"	=> $tablo,	"index2" => $statu,
        );
        echo json_encode($jsonData);
}

This function creates a separate table for each brand and the json is formed as follows

{"index1":"tables","index2":"1"}{"index1":"tables","index2":"1"}

If this json data is single, it works fine
Note: Is it possible to do something in the ajax code when there is no such thing as merging this data into the page_ajax.php page?

Thank you

Adem,
Json data is an “object” not an array. You could just use the array version instead.
Then, in the array, you would need to pull out just the tables you need. To do that, you add an arg to the json decode which creates an array instead of an object.

So, $json_array = json_decode($jsonData, true); Would create an array, named $json_array.
Then, inside that array, you would have the two results with the first index being “index1”…
To pull out the array data for that, of course, you can use $json_array[“index1”] …

Is that what you need?

Array version? Ajax?

I don’t fully understand your suggestion.

I use json a lot in php, I add sessions by converting arrays to json
I’m using two different data to use in two different fields when pulling data with ajax

$.ajax({
	url: "page_ajax.php",
	method: "POST",
	dataType: "json",
	data: { "data": "1", "data2": "5" },
	success: function (msg) {
	$.('.field1').html(data.index1);
	$.('.field2').html(data.index2);
	}

page.jax.php

        $jsonData = array(
            "index1"	=> $tablo,	"index2" => $statu,
        );
        echo json_encode($jsonData);

Where I use it now, the problem is that it loops as much as the number of brands and outputs separately for each brand, resulting in more than one result.

Well, I am not clear what you want. If you create the two tables, $tablo and $statu and pass both to the calling page, just change how you create them and pass them as you really need them to be.

Confused on what you what changed…

You can merge data, just combine them in a different manner. If you create an array, you get that array.
If you create a multi-dimensional array, you get that, too. Perhaps you need to understand how you created the $tablo and $statu and combine them using a different query. Then pass the data as one indexed array instead of two. Obviously, these two $tablo and $statu are each a table so they can
not be sent in json as one item. You need to combine them in your query and sent already combined.

Not sure if that made sense to you…

Thank you for your interest @ErnieAlex ,

There are 3 types of tables

  1. products table
  2. Out of stock chart from x brands
  3. Table that says this product is not required for the project

The 3rd table, a stand-alone table is coming (this is working now because it is the only table)

Table 1 and 2 may come together
example as json

{"index1":"tables","index2":"1"} // a brand product table
{"index1":"tables","index2":"2"} // b brand product is out of stock
{"index1":"tables","index2":"1"} // c brand product table
{"index1":"tables","index2":"1"} // d brand product table
{"index1":"tables","index2":"1"} // e brand product table
{"index1":"tables","index2":"2"} // f brand product is out of stock

These tables will come with index1
{"index1":"tables","index2":"3"} // means this product is not required for your project

What is index2?
It only returns a numeric value
1 means table of products
2 means the products are out of stock
3 means this product is not required for your project

or ask a question
Is it possible to get multiple curly braces data with ajax?
For example, can we put the above in page.ajax.php and get it with ajax?

Now that I think about it, can I do something like this?
data-status=“1”
We can find out whether the status is 1, 2 or 3 for the incoming given
Let me work on this a little :grinning:

I did what I wrote in the message above and I got the result.
@ErnieAlex sorry to bother you for nothing

No problem. We are here to help. But, remember that json data is basically just an array.
If you set up arrays inside arrays, then you would end up with multiple braces. Json is just a way
to send data from program to program.

1 Like

Yes, I also use it to move from page to page,
Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service