使用api数据创建动态表

时间:2014-10-09 09:19:48

标签: php html api

如何让我的表动态列出它从api中提取的所有值,atm它只提取1条记录并将其放入表中,其余数据放在它之外 < / p>

我可能在这里缺少一些非常小的东西,但不能把手指放在上面

感谢您的帮助,非常感谢。

<?php
$trackingId = $_POST['trackingId'];
if (isset($_POST['trackingId'])) {
    $fetch = json_decode(file_get_contents(sprintf('apigoeshere', $trackingId)));
    if ($fetch->status != 'OK') {
        echo sprintf('Error: %s', $fetch->message);
    } else {
        //example how to access statistics variable
        $inactiveAccounts = $fetch->statistics->inactiveAccounts;
        echo sprintf('Inactive accounts: %d' . "<br /><br />", $inactiveAccounts);
?>
<table border="1">
        <tr>
            <th>Account name</th>
            <th>Level</th>
        </tr>
        <?php
            //example how to display all accounts username
            foreach ($fetch->accounts as $account) {
     ?>
        <tr>
                <td><?php
                echo $account->username;
        ?></td>
            <td><?php
            echo $account->currentLevel;
        ?></td>
       </tr>
</table>
    <?php
        }
     }
}
?>

1 个答案:

答案 0 :(得分:0)

没有在正确的地方关闭foreach。请改用此代码

<?php
$trackingId = $_POST['trackingId'];
if (isset($_POST['trackingId'])) {

    $fetch = json_decode(file_get_contents(sprintf('apigoeshere', $trackingId)));

    if ($fetch->status != 'OK') {
        echo sprintf('Error: %s', $fetch->message);
    } else {
        //example how to access statistics variable
        $inactiveAccounts = $fetch->statistics->inactiveAccounts;
        echo sprintf('Inactive accounts: %d' . "<br /><br />", $inactiveAccounts);
?>
<table border="1">
    <tr>
        <th>Account name</th>
        <th>Level</th>
    </tr>
    <?php
        //example how to display all accounts username
        foreach ($fetch->accounts as $account) {
?>
   <tr>
        <td><?php
            echo $account->username;
?></td>
        <td><?php
            echo $account->currentLevel;
?></td>
    </tr>
    <?php
            }
    ?>
</table>
<?php

    }
}
?>