0

I am tring to create a table dinamically with the products of a Magento order using API result.

I have the following PHP code to dinamically generate a table with Json content

$data = json_decode($resp, true);

/*Initializing temp variable to design table dynamically*/
$temp = "<table>";
 
/*Defining table Column headers depending upon JSON records*/
$temp .= "<tr><th>Order ID</th>";
$temp .= "<th>Sku</th>";
$temp .= "<th>Nome</th>";
$temp .= "<th>Quantità</th></tr>";
 
/*Dynamically generating rows & columns*/
for($i = 0; $i < sizeof($data["items"]); $i++)
{
$temp .= "<tr>";
$temp .= "<td>" . $data["items"][$i]["items"] . "</td>";
$temp .= "<td>" . $data["items"][$i]["sku"] . "</td>";
$temp .= "<td>" . $data["items"][$i]["name"] . "</td>";
$temp .= "<td>" . $data["items"][$i]["qty_ordered"] . "</td>";
$temp .= "</tr>";
}
 
/*End tag of table*/
$temp .= "</table>";
 
/*Printing temp variable which holds table*/
echo $temp;

In this way is not working because the products are in the second deep "items" node (json)

{
    "items": [{
        "items": [{
            "name": "Product 1",
            "order_id": 54228,
            "qty_ordered": 2,
            "sku": "190289855757"
        }, {
            "name": "Product 2",
            "order_id": 54228,
            "qty_ordered": 1,
            "sku": "4062051502434"
        }]
    }]
}

Decoded version

Array ( [items] => Array ( [0] => Array ( [items] => Array ( [0] => Array ( [name] => Product 1 [order_id] => 54228 [qty_ordered] => 2 [sku] => 190289855757 ) [1] => Array ( [name] => Product 2 [order_id] => 54228 [qty_ordered] => 1 [sku] => 4062051502434 ) ) ) ) )

I used the following API call

https://mywebsite.com/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=entity_id&searchCriteria[filter_groups][0][filters][0][value]=1&searchCriteria[filter_groups][0][filters][0][condition_type]=eq&fields=items[entity_id]&fields=items[items[order_id,sku,name,qty_ordered]]&searchCriteria[pageSize]=1

Is there a way to get just the second node items result or a way to set the for so that it counts just the second deep node containing "item"

1 Answer 1

0

You need to use this code.

/*Dynamically generating rows & columns*/

foreach($data["items"] as $productdata)
{
    $temp .= "<tr>";
    $temp .= "<td>" . $productdata["sku"] . "</td>";
    $temp .= "<td>" . $productdata["name"] . "</td>";
    $temp .= "<td>" . $productdata["qty_ordered"] . "</td>";
    $temp .= "</tr>";
}
5
  • Using this solution returned me just a cell valorized with value "Array" see screesnhot nimb.ws/KDRBQx Commented Sep 30, 2021 at 6:32
  • I update the answer, try that one, that will work for you. Commented Sep 30, 2021 at 6:50
  • Thank you for your suggestion, unfortunately is giving an //Invalid argument supplied for foreach()** Commented Sep 30, 2021 at 7:01
  • I update the answer, can you check that thnigs, is that working or not. Commented Sep 30, 2021 at 9:04
  • Hi, it is not working on my side. I updated the post with the jeson response and decoded Commented Sep 30, 2021 at 9:21

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.