Print information from a php file that generates json

Asked

Viewed 32 times

1

Imagem do que é impresso ao entrar na tela

My.php data file generates a json on the screen with the database data, as I could do to take this generated json data and print it to a table for example?

  • 1

    Can your code and explain your question better.

  • You don’t even need to convert to JSON, you can simply go through the array and print the values.

1 answer

0

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

For example, it would look 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 change the json data. by the URL you have specified or create a script to save and use it, as I did in the example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.