Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

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

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...