How can I parse JSON into a html table using PHP?
Asked Answered
R

6

13

I have to get a table in my website. And have to get the data for this table from "http://west.basketball.nl/db/json/stand.pl?szn_Naam=2014-2015&cmp_ID=373" I've tried a lot of thing but nothing works....

 <!DOCTYPE html>
  <html>
   <head>
    <script type="text/javascript" 
       src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   </head>
   <body>
    <?php
    $json=file_get_contents("http://west.basketball.nl/db/json
    /stand.pl?szn_Naam=2014-2015&cmp_ID=373");
            $data =  json_decode($json);

        if (count($data)) {
            // Open the table
            echo "<table>";

            // Cycle through the array
            foreach ($data as $stand) {

                // Output a row
                echo "<tr>";
                echo "<td>$afko</td>";
                echo "<td>$positie</td>";
                echo "</tr>";
            }

            // Close the table
            echo "</table>";
        }
    ?>
  </body>
</html>
Ramin answered 6/10, 2014 at 9:9 Comment(7)
Sorry fellows... forgot to say but this is written in a .php fileRamin
What does not work? Have you set error_reporting to E_ALL and display_errors to on in your dev environement?Barbi
what do you mean?.... i guess not.... still new to phpRamin
I guess the problem is that url_fopen is set to disabled due to security reason. Add error_reporting(E_ALL); to the beginnig of your PHP script. And make sure in your .htaccess or php.ini that display_errors is set to On or have a view into the PHP log file after your request.Barbi
i dont know what a php.ini is and dont know what a .htaccess is so..... , i did add the error reporting but it still doesnt work. There is one error in the console, its about the drawing set of the html and how its not declared and about US-ASCCI or something?Ramin
You don't see PHP errors on the client side error console because PHP is a server side thing. You have to look into the PHP error log on your webserver to figure that out. Or you create a .htaccess file in the folder where your script is and put the following line into it: display_errors On.Barbi
This thread has been answered here Click hereGearldinegearshift
C
21

Ok first thing to do when getting data from an external source is to understand what is being returned.

So do

<?php
$json=file_get_contents("http://west.basketball.nl/db/json/stand.pl?szn_Naam=2014-2015&cmp_ID=373");
$data =  json_decode($json);

print_r($data);

Result:

stdClass Object
(
    [stand] => Array
        (
            [0] => stdClass Object
                (
                    [afko] => Risne Stars HS 1
                    [ID] => 2091
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 100.0
                    [punten] => 6
                    [tegenscore] => 149
                    [eigenscore] => 191
                    [datum] => 2014-10-05
                    [saldo] => 42
                    [team] => Risne Stars Heren 1
                    [positie] => 1
                )

            [1] => stdClass Object
                (
                    [afko] => D.B.V. Arriba HS 2
                    [ID] => 1813
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 0.0
                    [punten] => 0
                    [tegenscore] => 116
                    [eigenscore] => 102
                    [datum] => 2014-10-05
                    [saldo] => -14
                    [team] => D.B.V. Arriba Heren 2
                    [positie] => 10
                )

            [2] => stdClass Object
                (
                    [afko] => The Valley Bucketeers HS 2
                    [ID] => 2430
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 0.0
                    [punten] => 0
                    [tegenscore] => 177
                    [eigenscore] => 70
                    [datum] => 2014-10-05
                    [saldo] => -107
                    [team] => The Valley Bucketeers Heren 2
                    [positie] => 11
                )

            [3] => stdClass Object
                (
                    [afko] => Uitsmijters HS 2
                    [ID] => 2143
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 100.0
                    [punten] => 4
                    [tegenscore] => 79
                    [eigenscore] => 161
                    [datum] => 2014-10-05
                    [saldo] => 82
                    [team] => Uitsmijters Heren 2
                    [positie] => 2
                )

            [4] => stdClass Object
                (
                    [afko] => Picker Reds HS 1
                    [ID] => 2056
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 66.7
                    [punten] => 4
                    [tegenscore] => 193
                    [eigenscore] => 184
                    [datum] => 2014-10-05
                    [saldo] => -9
                    [team] => Picker Reds Heren 1
                    [positie] => 3
                )

            [5] => stdClass Object
                (
                    [afko] => Peatminers HS 2
                    [ID] => 6247
                    [status] => Actief
                    [gespeeld] => 1
                    [percentage] => 100.0
                    [punten] => 2
                    [tegenscore] => 36
                    [eigenscore] => 64
                    [datum] => 2014-10-05
                    [saldo] => 28
                    [team] => Peatminers Heren 2
                    [positie] => 4
                )

            [6] => stdClass Object
                (
                    [afko] => Jolly Jumpers HS 1
                    [ID] => 1994
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 50.0
                    [punten] => 2
                    [tegenscore] => 103
                    [eigenscore] => 119
                    [datum] => 2014-10-05
                    [saldo] => 16
                    [team] => Jolly Jumpers Heren 1
                    [positie] => 5
                )

            [7] => stdClass Object
                (
                    [afko] => TONEGO '65 HS 2
                    [ID] => 2120
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 50.0
                    [punten] => 2
                    [tegenscore] => 107
                    [eigenscore] => 122
                    [datum] => 2014-10-05
                    [saldo] => 15
                    [team] => TONEGO '65 Heren 2
                    [positie] => 6
                )

            [8] => stdClass Object
                (
                    [afko] => Amical HS 2
                    [ID] => 1791
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 33.3
                    [punten] => 2
                    [tegenscore] => 180
                    [eigenscore] => 195
                    [datum] => 2014-10-05
                    [saldo] => 15
                    [team] => Amical Heren 2
                    [positie] => 7
                )

            [9] => stdClass Object
                (
                    [afko] => S.V.Z.W. HS 2
                    [ID] => 5526
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 33.3
                    [punten] => 2
                    [tegenscore] => 174
                    [eigenscore] => 151
                    [datum] => 2014-10-05
                    [saldo] => -23
                    [team] => S.V.Z.W. Heren 2
                    [positie] => 8
                )

            [10] => stdClass Object
                (
                    [afko] => Twente Buzzards HS 3
                    [ID] => 2294
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 33.3
                    [punten] => 2
                    [tegenscore] => 196
                    [eigenscore] => 151
                    [datum] => 2014-10-05
                    [saldo] => -45
                    [team] => Twente Buzzards Heren 3
                    [positie] => 9
                )

        )

    [nummer] => OHS2C
    [version] => 1.0
    [aantal_teams] => 11
    [id] => 373
    [seizoen] => 2014-2015
    [naam] => Oost Afdeling Heren Senioren 2e klasse C
    [gewijzigd] => 2014-10-05 18:34:25
)

So now you know you are dealing with an OBJECT and not scalar values or an array.

So try this code:-

<?php
    $json=file_get_contents("http://west.basketball.nl/db/json/stand.pl?szn_Naam=2014-2015&cmp_ID=373");
    $data =  json_decode($json);

    if (count($data->stand)) {
        // Open the table
        echo "<table>";

        // Cycle through the array
        foreach ($data->stand as $idx => $stand) {

            // Output a row
            echo "<tr>";
            echo "<td>$stand->afko</td>";
            echo "<td>$stand->positie</td>";
            echo "</tr>";
        }

        // Close the table
        echo "</table>";
    }
?>
Coiffeur answered 6/10, 2014 at 9:33 Comment(1)
Wow Thank a lot it finally works :D Do you know a way to make it sort bij the positions? 1.2.3.4.5.6.7.8.9.10.11 and so on so onRamin
D
27

If you want recursive way:

public static function jsonToDebug($jsonText = '')
{
    $arr = json_decode($jsonText, true);
    $html = "";
    if ($arr && is_array($arr)) {
        $html .= self::_arrayToHtmlTableRecursive($arr);
    }
    return $html;
}

private static function _arrayToHtmlTableRecursive($arr) {
    $str = "<table><tbody>";
    foreach ($arr as $key => $val) {
        $str .= "<tr>";
        $str .= "<td>$key</td>";
        $str .= "<td>";
        if (is_array($val)) {
            if (!empty($val)) {
                $str .= self::_arrayToHtmlTableRecursive($val);
            }
        } else {
            $str .= "<strong>$val</strong>";
        }
        $str .= "</td></tr>";
    }
    $str .= "</tbody></table>";

    return $str;
}

Then call echo YourClass::jsonToDebug($jsonText);

My test on http://sandbox.onlinephpfunctions.com/

Dubai answered 21/4, 2016 at 5:34 Comment(2)
Best answer, since it's 1) structure-agnostic and 2) code-efficient (recursive)Lindly
Great! You could also change $str .= "<strong>$val</strong>"; to $str .= '<strong>' . var_export($val, true) . '</strong>'; to display falsey values, etc.Respiration
C
21

Ok first thing to do when getting data from an external source is to understand what is being returned.

So do

<?php
$json=file_get_contents("http://west.basketball.nl/db/json/stand.pl?szn_Naam=2014-2015&cmp_ID=373");
$data =  json_decode($json);

print_r($data);

Result:

stdClass Object
(
    [stand] => Array
        (
            [0] => stdClass Object
                (
                    [afko] => Risne Stars HS 1
                    [ID] => 2091
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 100.0
                    [punten] => 6
                    [tegenscore] => 149
                    [eigenscore] => 191
                    [datum] => 2014-10-05
                    [saldo] => 42
                    [team] => Risne Stars Heren 1
                    [positie] => 1
                )

            [1] => stdClass Object
                (
                    [afko] => D.B.V. Arriba HS 2
                    [ID] => 1813
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 0.0
                    [punten] => 0
                    [tegenscore] => 116
                    [eigenscore] => 102
                    [datum] => 2014-10-05
                    [saldo] => -14
                    [team] => D.B.V. Arriba Heren 2
                    [positie] => 10
                )

            [2] => stdClass Object
                (
                    [afko] => The Valley Bucketeers HS 2
                    [ID] => 2430
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 0.0
                    [punten] => 0
                    [tegenscore] => 177
                    [eigenscore] => 70
                    [datum] => 2014-10-05
                    [saldo] => -107
                    [team] => The Valley Bucketeers Heren 2
                    [positie] => 11
                )

            [3] => stdClass Object
                (
                    [afko] => Uitsmijters HS 2
                    [ID] => 2143
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 100.0
                    [punten] => 4
                    [tegenscore] => 79
                    [eigenscore] => 161
                    [datum] => 2014-10-05
                    [saldo] => 82
                    [team] => Uitsmijters Heren 2
                    [positie] => 2
                )

            [4] => stdClass Object
                (
                    [afko] => Picker Reds HS 1
                    [ID] => 2056
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 66.7
                    [punten] => 4
                    [tegenscore] => 193
                    [eigenscore] => 184
                    [datum] => 2014-10-05
                    [saldo] => -9
                    [team] => Picker Reds Heren 1
                    [positie] => 3
                )

            [5] => stdClass Object
                (
                    [afko] => Peatminers HS 2
                    [ID] => 6247
                    [status] => Actief
                    [gespeeld] => 1
                    [percentage] => 100.0
                    [punten] => 2
                    [tegenscore] => 36
                    [eigenscore] => 64
                    [datum] => 2014-10-05
                    [saldo] => 28
                    [team] => Peatminers Heren 2
                    [positie] => 4
                )

            [6] => stdClass Object
                (
                    [afko] => Jolly Jumpers HS 1
                    [ID] => 1994
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 50.0
                    [punten] => 2
                    [tegenscore] => 103
                    [eigenscore] => 119
                    [datum] => 2014-10-05
                    [saldo] => 16
                    [team] => Jolly Jumpers Heren 1
                    [positie] => 5
                )

            [7] => stdClass Object
                (
                    [afko] => TONEGO '65 HS 2
                    [ID] => 2120
                    [status] => Actief
                    [gespeeld] => 2
                    [percentage] => 50.0
                    [punten] => 2
                    [tegenscore] => 107
                    [eigenscore] => 122
                    [datum] => 2014-10-05
                    [saldo] => 15
                    [team] => TONEGO '65 Heren 2
                    [positie] => 6
                )

            [8] => stdClass Object
                (
                    [afko] => Amical HS 2
                    [ID] => 1791
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 33.3
                    [punten] => 2
                    [tegenscore] => 180
                    [eigenscore] => 195
                    [datum] => 2014-10-05
                    [saldo] => 15
                    [team] => Amical Heren 2
                    [positie] => 7
                )

            [9] => stdClass Object
                (
                    [afko] => S.V.Z.W. HS 2
                    [ID] => 5526
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 33.3
                    [punten] => 2
                    [tegenscore] => 174
                    [eigenscore] => 151
                    [datum] => 2014-10-05
                    [saldo] => -23
                    [team] => S.V.Z.W. Heren 2
                    [positie] => 8
                )

            [10] => stdClass Object
                (
                    [afko] => Twente Buzzards HS 3
                    [ID] => 2294
                    [status] => Actief
                    [gespeeld] => 3
                    [percentage] => 33.3
                    [punten] => 2
                    [tegenscore] => 196
                    [eigenscore] => 151
                    [datum] => 2014-10-05
                    [saldo] => -45
                    [team] => Twente Buzzards Heren 3
                    [positie] => 9
                )

        )

    [nummer] => OHS2C
    [version] => 1.0
    [aantal_teams] => 11
    [id] => 373
    [seizoen] => 2014-2015
    [naam] => Oost Afdeling Heren Senioren 2e klasse C
    [gewijzigd] => 2014-10-05 18:34:25
)

So now you know you are dealing with an OBJECT and not scalar values or an array.

So try this code:-

<?php
    $json=file_get_contents("http://west.basketball.nl/db/json/stand.pl?szn_Naam=2014-2015&cmp_ID=373");
    $data =  json_decode($json);

    if (count($data->stand)) {
        // Open the table
        echo "<table>";

        // Cycle through the array
        foreach ($data->stand as $idx => $stand) {

            // Output a row
            echo "<tr>";
            echo "<td>$stand->afko</td>";
            echo "<td>$stand->positie</td>";
            echo "</tr>";
        }

        // Close the table
        echo "</table>";
    }
?>
Coiffeur answered 6/10, 2014 at 9:33 Comment(1)
Wow Thank a lot it finally works :D Do you know a way to make it sort bij the positions? 1.2.3.4.5.6.7.8.9.10.11 and so on so onRamin
B
1

Why not try :

$data = json_decode($json, true);
Brahms answered 6/10, 2014 at 10:4 Comment(1)
Explanation: the second argument (true) tells json_decode() to return an associative array rather than object of the given data which can be more intuitive to work with if you're not into OO programming.Pinko
P
1

Old post, but a simpler solution:

echo '<pre>'. print_r(  json_decode($json), true)).'</pre>';
  • php json_decode: unserialize json string
  • php print_r: print object content, using CRLF
  • html <pre>: show text as HTML

You get your json in HTML on your browser like:

stdClass Object
(
    [field_1] => 1
    [field_2] => teste
    [other] => aaaa
    [another] => bbbb
)
Pasqualepasqueflower answered 18/3, 2024 at 17:7 Comment(0)
E
0

I guess, the issue is with the variables you are using - $afko and $positie. Please try with the below code -

// Cycle through the array
foreach ($data as $stand) {
    // Output a row
    echo "<tr>";
    echo "<td> . $stand['afko'] . </td>";
    echo "<td> . $stand['positie'] . </td>";
    echo "</tr>";
}
Errecart answered 6/10, 2014 at 9:23 Comment(0)
K
0

This code below can be useful for dynamically converting your entire table without having to define column names.

 echo "<table>";
        echo "<tr>";
        foreach(array_keys($your_array[0]) as $head){
            echo "<td>$head</td>";
        }
        echo "</tr>";

        foreach($your_array as $row){
            echo "<tr>";            
            foreach($row as $col){
                echo "<td>$col</td>";
            }
            echo "</tr>";
        }
        echo "</table>";

        return $posicoes;
Koon answered 14/3, 2020 at 23:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.