通过 PHP 将 JSON 输出转换为 HTML 表


Converting JSON output to HTML table through PHP?

我目前不知道如何做到这一点,需要帮助,但我目前正在构建一个离子应用程序,并在通过PHP脚本获取信息时将信息存储在数据库中,我正在接收JSON,我想将其转换为一个漂亮的格式化HTML表格

这是我的PHP

<?php           
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Origin: *");
$host = "****";
$user = "*****";
$password = "";
$database = "holes";
$connect = mysqli_connect($host,$user,$password,$database) or die("Problem connecting.");
$result = mysqli_query($connect, "SELECT * from hole18") or die("Bad Query.");
mysqli_close($connect);
$results = array();
while($row = mysqli_fetch_assoc($result))
{
    $results[] = $row; 
}
echo json_encode($results);
    ?>

这是 Json 输出:

 [{"index":"1","Par":"4","FIR":"1","GIR":"1","Score":"3","puttsno":"1","bunkershit":"0","oob":"0"}]

请帮助我

试试这个

<table>
<?php
    $json_result = json_encode($results); 
    $json_dec_result = json_decode( $json_result );
    if ( !empty( $json_dec_result) ) 
    {
        foreach ($json_dec_result as $field_name => $field_value) 
        {
            ?>
                <tr>
                    <td>
                        <?php echo  $field_name;?>
                    </td>
                    <td>
                        <?php echo  $field_value;?>
                    </td>
                </tr>   
            <?php 
        }
    }
?>
</table>

Uttam的答案将用于从PHP代码中获取HTML表服务器响应。这可能是您所需要的,但是如果您使用的是Ionic,那么更合适的方法可能是仍然坚持使用JSON响应,将其加载到控制器中,并使用ng-repeat和数据网格将其显示为HTML。

如果您有兴趣,请参阅以下内容:在 ionic 中创建表格