Home > Software engineering >  How to loop a JSON nested Array into table list
How to loop a JSON nested Array into table list

Time:09-17

Hi I am trying to read a nested JSON array data into a table and as a list not just in a line. Look, at the way it comes out: Problem - it's just one lone line

Link to JSON: JSON L: demo P: ocfB6XzF73 Documentation: URL-DOC

JSON code I am trying to reach:

"EquipmentList": [ "ABSBrakes", "Alarm", "AlloyRims", "AntiSpin", "AutomaticGear", "RemoteCentralLocking", "PoweredWindows", "PoweredMirrorsHeated", "CruiseControl", "InfoCenter", "AutomaticClimateControl", "TripComputer", "Navigation", "GearShiftStearingWheel", "ServiceOK", "Immobilizer", "SeatHeater", "XenonLight" ]

My code so far i very simple as I struggle to list the data to a list i.e. LI or a TABLE :

    <?php
$url = 'https://gw.bilinfo.net/listingapi/api/export';

// provide your username and password here
$auth = base64_encode("demo:ocfB6XzF73");

// create HTTP context with basic auth
$context = stream_context_create([
    'http' => ['header' => "Authorization: Basic $auth"]
]);

// query for data
$data = file_get_contents($url, false, $context);
// $escaped = json_encode($data);
$escaped = json_decode($data); //, JSON_FORCE_OBJECT

/*Initializing temp variable to design table dynamically*/
$temp = "<table>";

/*Defining table Column headers depending upon JSON records*/
$temp .= "<tr>";
$temp .= "<th>Bil</th>";
$temp .= "<th>Model</th>";
$temp .= "<th>Motor</th>";
$temp .= "<th>Drivmiddel</th>";
$temp .= "<th>Udstyr</th>";
$temp .= "<th>Billeder</th>";
$temp .= "</tr>";


/*Dynamically generating rows & columns*/
foreach ($escaped->Vehicles as $vehicle) {
    $temp .= "<tr>";
    $temp .= "<td>" . $vehicle->Make . "</td>";
    $temp .= "<td>" . $vehicle->Model . "</td>";
    $temp .= "<td>" . $vehicle->Motor . "</td>";
    $temp .= "<td>" . $vehicle->Propellant . "</td>";


    foreach($escaped->Vehicles[0]->EquipmentList as $EquipmentItem){
        $temp .= "<td>" . $EquipmentItem . "</td>";
    }
    for ($p = 0; $p < $vehicle->PictureCount; $p  ) {
        $temp .= "<td><img src='" . $vehicle->Pictures[0] . "'></td>";
    }
}
$temp .= "</tr>";
/*End tag of table*/
$temp .= "</table>";

/*Printing temp variable which holds table*/
echo $temp;

echo $data;
?>

CodePudding user response:

With the help of Anand Pandey

<?php
$url = 'https://gw.bilinfo.net/listingapi/api/export';

// provide your username and password here
$auth = base64_encode("demo:ocfB6XzF73");

// create HTTP context with basic auth
$context = stream_context_create([
    'http' => ['header' => "Authorization: Basic $auth"]
]);

// query for data
$data = file_get_contents($url, false, $context);
// $escaped = json_encode($data);
$escaped = json_decode($data); //, JSON_FORCE_OBJECT

/*Initializing temp variable to design table dynamically*/
$temp = "<table class='car-list'>";

/*Defining table Column headers depending upon JSON records*/
$temp .= "<tr>";
$temp .= "<th class='th-style'>Bil</th>";
$temp .= "<th class='th-style'>Model</th>";
$temp .= "<th class='th-style'>Motor</th>";
$temp .= "<th class='th-style'>Drivmiddel</th>";
$temp .= "<th class='th-style'>Udstyr</th>";
$temp .= "<th class='th-style'>Billeder</th>";
$temp .= "</tr>";


/*Dynamically generating rows & columns*/
foreach ($escaped->Vehicles as $vehicle) {
    $temp .= "<tr>";
    $temp .= "<td class='td-style'>" . $vehicle->Make . "</td>";
    $temp .= "<td class='td-style'>" . $vehicle->Model . "</td>";
    $temp .= "<td class='td-style'>" . $vehicle->Motor . "</td>";
    $temp .= "<td class='td-style'>" . $vehicle->Propellant . "</td>";


    $temp .= "<td class='td-style'>";
    foreach ($escaped->Vehicles[0]->EquipmentList as $EquipmentItem) {
        $temp .= "<table>";
        $temp .= "<tr>";
        $temp .= "<td class='equipmentlist'>" . $EquipmentItem . "</td>";
        $temp .= "</tr>";
        $temp .= "</table>";
    }
    $temp .= "</td>";
    $temp .= "<td class='td-style'>";
    for ($p = 0; $p < $vehicle->PictureCount; $p  ) {
        $temp .= "<table>";
        $temp .= "<tr>";
        $temp .= "<td class='td-style'>";
        $temp .= "<img class='cc-images' src='" . $vehicle->Pictures[$p] . "'>";
        $temp .= "</td>";
        $temp .= "</tr>";
        $temp .= "</table>";
    }
    $temp .= "</td>";
}
$temp .= "</tr>";
/*End tag of table*/
$temp .= "</table>";

/*Printing temp variable which holds table*/
echo $temp;

//echo $data;
?>
  • Related