Home > Software engineering >  How to get loop JSON all of Array (from URL) into a HTML Table with php
How to get loop JSON all of Array (from URL) into a HTML Table with php

Time:09-17

I am trying to get the EquipmentList from the Vehicles list in the JSON URL: JSON link

Look at documentation here: Link to documentation

I am trying to add the EquipmentList so that it appears in the table like the images... Just for now at least...

My code so far:

      <?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>";

    for ($e = 0; $e < $vehicle->EquipmentList; $e  ) {
        $temp .= "<td>" . $vehicle->EquipmentList[0] . "</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;
?>


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

CodePudding user response:

does $EquipmentList correctly describe the values inside the variable?
Perhaps $EquipmentItem is more correct?

if so, it will return an object (json)
for example

{
    "name": "Motor 1 A",
    "img": "/someimg.jpg"
}

to display an image, you need <img/> tag

foreach($escaped->Vehicles[0]->EquipmentList as $EquipmentItem){
    $temp .= "<td>" . "<img src='" . $EquipmentItem->img . "' />" . "</td>";
}

Based on your example, the correct solution may vary, but this should give you idea if the correct question has been answered.

CodePudding user response:

I modified Phillip Zoghbi's code - removed the image part c;) - So in part Phillip is right!

        foreach($escaped->Vehicles[0]->EquipmentList as $EquipmentItem){
            $temp .= "<td>" . $EquipmentItem . "</td>";
    }
  • Related