简体   繁体   中英

Display Php (json) output in tabular format

I am new to Wordpress Plugin Developement,Wrote a function that requests a url for data and gets the same in Json Format,The Function Snippet is as follows;

function user_det()
{
$em = '';
$url = ''.$em;
$myhtml = wp_remote_retrieve_body( wp_remote_get($url, array( 'timeout'=>120)));
echo '<pre>' , print_r($myhtml) , '</pre>';
}

The Output:

{

"Status":"Success",
"Bookings":[
    {
        "Message":"",
        "Detail":{
            "ServiceType":"",
            "HoName":"",
            "HAddress1":"",
            "ToRooms":"",
            "RoDetail":[
                {
                    "RoomTypeDescription":[
                        " "
                    ],
                    "NumberOfRooms":"",
                    "Passengers":[
                        {
                            "Salutations":"",
                            "FirstName":"",
                            "LastName":""                         
                        },
                        {
                            "Salutations":"",

                            "Age":"",
                            "CotBedOption":""
                        }
                    ],
                    "totalAdult":2,
                    "totalChild":0
                }
            ],
            "Phone":"-",
            "Rating":"",
            "email":""
        }
    }]}

However ,Is there a way where I could Display the data in a tabular format,tried searching online but could not understand

As you are getting the information in JSON format from your API request, you'll need to iterate each element to format the way you want, example of building a table using this information.

$myhtml = wp_remote_retrieve_body( wp_remote_get($url, array( 'timeout'=>120)));
$dataArray = json_decode($myhtml,1);
echo '<table>';
echo '<thead><tr><th>Service Type</th></tr></thead>';
echo '<tbody>';
// Now we have the json converted into an array, let's iterate it
foreach($dataArray["Bookings"] as $idx=>$bookingData) {
    // Iterate all bookings and write on the rows
    echo '<tr>';
        echo '<td>'.$bookingData['Detail']['ServiceType'].'</td>';
    echo '</tr>';
}
echo '</tbody><table>';

I believe this gives you the idea on what you need to perform to get your object and build a table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM