【问题标题】:Fusion Table Response into HTML Table将表格响应融合到 HTML 表格中
【发布时间】:2014-01-19 16:20:40
【问题描述】:

我正在尝试将 Fusion Table SQL 响应放入基本的 HTML 表中。这既适用于搜索引擎素材,也适用于谷歌电子表格及其 importhtml 功能。

将响应转换为表格的 foreach 出现了一些不寻常的响应,例如一次 1 个字符?此外,响应似乎被格式化为可以轻松制作成数组的东西,但我的努力是徒劳的。现在已经研究了两天多,我敢打赌有人比我更了解格式并且知道答案吗?

<?php
$result = file_get_contents("https://www.googleapis.com/fusiontables/v1/query?sql=SELECT%20*%20FROM%201QN6e86FybBULPekKvvXd_RF1jw01H7bZAJFjhUg&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ");
?>
<!DOCTYPE html>
<html>
<body>
<div id="page">
    <h1>A Table of Clicks</h1>

    <table class='data'>
        <thead>
            <tr>
                <th>date</th>
                <th>fundraiser</th>
                <th>name</th>
                <th>link</th>
                <th>price</th>
                <th>image</th>
                <th>ip</th>
                <th>merchant</th>
            </tr>
        </thead>
        <tbody>
<?php
list($part1, $part2) = explode(' "rows": [[', $result);
$rows = explode('  ], [', $part2);
echo $rows[0]."<br>";
foreach ($rows as $row) {
{
//$array = array($row);
///var_dump($array);
$boxes = explode(",", $row);
    foreach ($boxes as $box) {
        echo "
        <tr>
        <td>".$box[0]."</td>
        <td>".$box[1]."</td>
        <td>".$box[2]."</td>
        <td>".$box[3]."</td>
        <td>".$box[4]."</td>
        <td>".$box[5]."</td>
        <td>".$box[6]."</td>
        <td>".$box[7]."</td>
        </tr>";
    }
    }
}
?>
        </tbody>
    </table>
    <hr />
    </div> 
</body>

【问题讨论】:

    标签: php html-table google-fusion-tables


    【解决方案1】:

    使用json_decode解析响应(它是有效的JSON):

    <?php
    $result = file_get_contents("https://www.googleapis.com/fusiontables/v1/query?sql=SELECT%20*%20FROM%201QN6e86FybBULPekKvvXd_RF1jw01H7bZAJFjhUg&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ");
    $result=json_decode($result,true);
    ?>
    <!DOCTYPE html>
    <html>
    <body>
    <div id="page">
        <h1>A Table of Clicks</h1>
    
        <table class='data'>
            <thead>
                <tr>
                    <th><?php echo implode('</th><th>',$result['columns']);?></th>
                </tr>
            </thead>
            <tbody>
              <?php
                foreach($result['rows'] as $row){
                  echo '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
                }
              ?>
            </tbody>
        </table>
        <hr />
        </div> 
    </body>
    

    【讨论】:

      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2019-06-10
      • 2015-09-05
      • 2020-05-07
      • 1970-01-01
      • 2021-04-23
      相关资源
      最近更新 更多