Print information from a php file that generates json [closed]

1

My data.php file generates a json on the screen with the database's data, how could I get the generated json data and print it on a table for example?

    
asked by anonymous 19.08.2018 / 07:58

1 answer

0

Very simple. You can use: file_get_contents (or CURL) , json_decode and a foreach

For example, it looks like this:

<?php

$json = file_get_contents("dados.json");
$dados = json_decode($json, TRUE);

echo "<table>";
echo "<tbody>";

echo "<tr>";
echo "<td>ID</td>";
echo "<td>Empresa</td>";
echo "<td>Valor</td>";
echo "</tr>";

foreach ($dados as $info) {
    echo "<tr>";
    echo "<td>" . $info["id"] . "</td>";
    echo "<td>" . $info["empresa"] . "</td>";
    echo "<td>" . $info["valor"] . "</td>";
    echo "</tr>";
}

echo "</tbody>";
echo "</table>"; 

?>

You can either change the data.json in the script by the URL you entered or create a script to save it and use it as I did in the example.

    
19.08.2018 / 17:26