Home > OS >  How to populate multi object json data as html table using php /jquiry?
How to populate multi object json data as html table using php /jquiry?

Time:10-01

I have a (PHP cURL) Shipment Tracking API from Rapidapi. the code i have to place in my website is below.

=== the api code received from rapidapi ===
<?php

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => "https://order-tracking.p.rapidapi.com/trackings/realtime",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => "{\n    \"tracking_number\": \"CM419380522IN \",\n    \"carrier_code\": \"india-post\"\n}",
    CURLOPT_HTTPHEADER => [
        "content-type: application/json",
        "x-rapidapi-host: order-tracking.p.rapidapi.com",
        "x-rapidapi-key: <YOUR-RAPID-API-KEY>"
    ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}else {
    echo $response;
}

when I save this code in php file and refresh the page I get the result like:

default result

=== api result ===
{"meta":{"code":200,"type":"Success","message":"Success"},"data":{"items":[{"id":"88ccbd9812965e638ffc868a0783fed4","tracking_number":"CM419380522IN","carrier_code":"india-post","order_create_time":"","destination_code":"","status":"transit","track_update":true,"original_country":"India","itemTimeLength":9,"stayTimeLength":2,"service_code":null,"packageStatus":null,"substatus":null,"last_mile_tracking_supported":null,"origin_info":{"ItemReceived":"2021-09-20 15:56:31","ItemDispatched":null,"DepartfromAirport":null,"ArrivalfromAbroad":null,"CustomsClearance":null,"DestinationArrived":null,"weblink":"http:\/\/www.indiapost.gov.in\/","phone":null,"carrier_code":"india-post","trackinfo":[{"StatusDescription":"Item Received","Date":"2021-09-28 11:07:30","Details":"Timarni S.O","checkpoint_status":"transit","substatus":"transit001"},{"StatusDescription":"Item Dispatched","Date":"2021-09-25 17:40:05","Details":"ITARSI RS TMO","checkpoint_status":"transit","substatus":"transit002"},{"StatusDescription":"Item Received","Date":"2021-09-25 16:54:35","Details":"citynameS RS TMO","checkpoint_status":"transit","substatus":"transit001"},{"StatusDescription":"Item Dispatched","Date":"2021-09-25 16:47:10","Details":"citynameS PH","checkpoint_status":"transit","substatus":"transit002"},{"StatusDescription":"Item Bagged","Date":"2021-09-25 16:45:58","Details":"Itarsi PH","checkpoint_status":"transit","substatus":"transit001"},{"StatusDescription":"Item Received","Date":"2021-09-25 13:36:10","Details":"Itarsi PH","checkpoint_status":"transit","substatus":"transit001"},{"StatusDescription":"Item Dispatched","Date":"2021-09-24 20:01:58","Details":"ITARSI RS TMO","checkpoint_status":"transit","substatus":"transit002"},{"StatusDescription":"Item Received","Date":"2021-09-24 19:56:54","Details":"ITARSI RS TMO","checkpoint_status":"transit","substatus":"transit001"},{"StatusDescription":"Item Dispatched","Date":"2021-09-24 15:35:18","Details":"Bhopal RS TMO","checkpoint_status":"transit","substatus":"transit002"},{"StatusDescription":"Item Received","Date":"2021-09-24 15:23:42","Details":"Bhopal RS TMO","checkpoint_status":"transit","substatus":"transit001"},{"StatusDescription":"Item Dispatched","Date":"2021-09-24 12:51:37","Details":"Bhopal PH","checkpoint_status":"transit","substatus":"transit002"},{"StatusDescription":"Item Bagged","Date":"2021-09-24 11:35:34","Details":"Bhopal PH","checkpoint_status":"transit","substatus":"transit001"},{"StatusDescription":"Item Received","Date":"2021-09-24 09:56:16","Details":"Bhopal PH","checkpoint_status":"transit","substatus":"transit001"},{"StatusDescription":"Item Dispatched","Date":"2021-09-24 01:40:23","Details":"Bhopal RS TMO","checkpoint_status":"transit","substatus":"transit002"},{"StatusDescription":"Item Received","Date":"2021-09-23 21:12:22","Details":"Bhopal RS TMO","checkpoint_status":"transit","substatus":"transit001"},{"StatusDescription":"Item Dispatched","Date":"2021-09-22 22:07:24","Details":"Pune PH","checkpoint_status":"transit","substatus":"transit002"},{"StatusDescription":"Item Bagged","Date":"2021-09-22 21:31:36","Details":"Pune PH","checkpoint_status":"transit","substatus":"transit001"},{"StatusDescription":"Item Received","Date":"2021-09-22 19:47:06","Details":"Pune PH","checkpoint_status":"transit","substatus":"transit001"},"substatus":"notfound001","checkpoint_status":"transit"}]},"destination_info":{"ItemReceived":null,"ItemDispatched":null,"DepartfromAirport":null,"ArrivalfromAbroad":null,"CustomsClearance":null,"DestinationArrived":null,"weblink":null,"phone":null,"carrier_code":null,"trackinfo":null},"lastEvent":"Item Received,Timarni S.O,2021-09-28 11:07:30","lastUpdateTime":"2021-09-28 11:07:30"}]}}

so I want to get the above data (json/array) into an html table and I want to change the tracking id from CM419380522IN to <?php echo $tracking_id; ?> php string, so it will change dynamically.

I have tried converting the above data into array and tried to populate in table using the code below but got no success.

  $arr = json_decode($response, true);

//  echo "<pre>";
//  print_r($arr);
// echo "<pre>";

 echo '<table>';

 foreach ($arr as list ("id" => $id, "tracking_number" => $date, "Details" => $city, "checkpoint_status" => $status, "substatus" => $live,)) { 
    echo "<tr><td>{$id}</td><td>{$date}</td><td>{$city}</td><td>{$status}</td><td>{$live}</td></tr>";
 }
 
 echo '</table>';

$arr echo "<pre>"; print_r($arr); echo "<pre>"; result is like :

Array
(
    [meta] => Array
        (
            [code] => 200
            [type] => Success
            [message] => Success
        )

    [data] => Array
        (
            [items] => Array
                (
                    [0] => Array
                        (
                            [id] => 88ccbd9812965e638ffc868a0783fed4
                            [tracking_number] => CM419380522IN
                            [carrier_code] => india-post
                            [order_create_time] => 
                            [destination_code] => 
                            [status] => transit
                            [track_update] => 1
                            [original_country] => India
                            [itemTimeLength] => 10
                            [stayTimeLength] => 2
                            [service_code] => 
                            [packageStatus] => 
                            [substatus] => 
                            [last_mile_tracking_supported] => 
                            [origin_info] => Array
                                (
                                    [ItemReceived] => 2021-09-20 15:56:31
                                    [ItemDispatched] => 
                                    [DepartfromAirport] => 
                                    [ArrivalfromAbroad] => 
                                    [CustomsClearance] => 
                                    [DestinationArrived] => 
                                    [weblink] => http://www.indiapost.gov.in/
                                    [phone] => 
                                    [carrier_code] => india-post
                                    [trackinfo] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [StatusDescription] => Item Received
                                                    [Date] => 2021-09-28 11:07:30
                                                    [Details] => Timarni S.O
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit001
                                                )

                                            [1] => Array
                                                (
                                                    [StatusDescription] => Item Dispatched
                                                    [Date] => 2021-09-25 17:40:05
                                                    [Details] => ITARSI RS TMO
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit002
                                                )

                                            [2] => Array
                                                (
                                                    [StatusDescription] => Item Received
                                                    [Date] => 2021-09-25 16:54:35
                                                    [Details] => ITARSI RS TMO
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit001
                                                )

                                            [3] => Array
                                                (
                                                    [StatusDescription] => Item Dispatched
                                                    [Date] => 2021-09-25 16:47:10
                                                    [Details] => Itarsi PH
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit002
                                                )

                                            [4] => Array
                                                (
                                                    [StatusDescription] => Item Bagged
                                                    [Date] => 2021-09-25 16:45:58
                                                    [Details] => Itarsi PH
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit001
                                                )

                                            [5] => Array
                                                (
                                                    [StatusDescription] => Item Received
                                                    [Date] => 2021-09-25 13:36:10
                                                    [Details] => Itarsi PH
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit001
                                                )

                                            [6] => Array
                                                (
                                                    [StatusDescription] => Item Dispatched
                                                    [Date] => 2021-09-24 20:01:58
                                                    [Details] => ITARSI RS TMO
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit002
                                                )

                                            [7] => Array
                                                (
                                                    [StatusDescription] => Item Received
                                                    [Date] => 2021-09-24 19:56:54
                                                    [Details] => ITARSI RS TMO
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit001
                                                )

                                            [8] => Array
                                                (
                                                    [StatusDescription] => Item Dispatched
                                                    [Date] => 2021-09-24 15:35:18
                                                    [Details] => Bhopal RS TMO
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit002
                                                )

                                            [9] => Array
                                                (
                                                    [StatusDescription] => Item Received
                                                    [Date] => 2021-09-24 15:23:42
                                                    [Details] => Bhopal RS TMO
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit001
                                                )

                                            [10] => Array
                                                (
                                                    [StatusDescription] => Item Dispatched
                                                    [Date] => 2021-09-24 12:51:37
                                                    [Details] => Bhopal PH
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit002
                                                )

                                            [11] => Array
                                                (
                                                    [StatusDescription] => Item Bagged
                                                    [Date] => 2021-09-24 11:35:34
                                                    [Details] => Bhopal PH
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit001
                                                )

                                            [12] => Array
                                                (
                                                    [StatusDescription] => Item Received
                                                    [Date] => 2021-09-24 09:56:16
                                                    [Details] => Bhopal PH
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit001
                                                )

                                            [13] => Array
                                                (
                                                    [StatusDescription] => Item Dispatched
                                                    [Date] => 2021-09-24 01:40:23
                                                    [Details] => Bhopal RS TMO
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit002
                                                )

                                            [14] => Array
                                                (
                                                    [StatusDescription] => Item Received
                                                    [Date] => 2021-09-23 21:12:22
                                                    [Details] => Bhopal RS TMO
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit001
                                                )

                                            [15] => Array
                                                (
                                                    [StatusDescription] => Item Dispatched
                                                    [Date] => 2021-09-22 22:07:24
                                                    [Details] => Pune PH
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit002
                                                )

                                            [16] => Array
                                                (
                                                    [StatusDescription] => Item Bagged
                                                    [Date] => 2021-09-22 21:31:36
                                                    [Details] => Pune PH
                                                    [checkpoint_status] => transit
                                                    [substatus] => transit001
                                                )

                                           

                                        )

                                )

                            [destination_info] => Array
                                (
                                    [ItemReceived] => 
                                    [ItemDispatched] => 
                                    [DepartfromAirport] => 
                                    [ArrivalfromAbroad] => 
                                    [CustomsClearance] => 
                                    [DestinationArrived] => 
                                    [weblink] => 
                                    [phone] => 
                                    [carrier_code] => 
                                    [trackinfo] => 
                                )

                            [lastEvent] => Item Received,Timarni S.O,2021-09-28 11:07:30
                            [lastUpdateTime] => 2021-09-28 11:07:30
                        )

                )

        )

)

screenshot of $arr

How to populate this json data into html table?

CodePudding user response:

Your foreach() syntax is wrong, try :

$arr = json_decode($response, true);

echo '<table>';
$keys = array_keys($arr[0]) ;
foreach ($arr as $line){
echo "<tr>" ;
    foreach ($keys AS $key){
        echo "<td>{$line['$key']}</td>" ;
    }
    echo "</tr>" ;
}
 echo '</table>';

With this script, we catch all the columns with $keys = array_keys($arr[0]) ; so we can loop one the exact array keys (assuming all raws have same structure).

CodePudding user response:

First, as I mentioned above, your array is not $arr, but a child of a child of $arr.

Second, not all the variables you request are actually children of the items array, some of them are children of multiple objects contained in an array that's a child of a child of your array.

Since you've specified in a comment that what you want outputted is every line of tracking, here's how you can do that:

$arr = json_decode($response, true);

// Now get the items array from $arr
$items = $arr['data']['items'];

echo '<table>';

// Loop through $items
foreach ($items as $item) {

    // Loop through $item['origin_info']['trackinfo']
    foreach ($item['origin_info']['trackinfo'] as $step) {
        echo '<tr>';

        // 'id' is an element of $item
        echo '<td>' . $item['id'] . '</td>';

        // 'tracking_number' is an element of $item
        echo '<td>' . $item['tracking_number'] . '</td>';

        // 'Details' is an element of $step
        echo '<td>' . $step['Details'] . '</td>';

        // 'checkpoint_status' is an element of $step
        echo '<td>' . $step['checkpoint_status'] . '</td>';

        // 'substatus' is an element of $step
        echo '<td>' . $step['substatus'] . '</td>';

    }


    echo '</tr>';
}
echo '</table>';
  • Related