简体   繁体   English

使用api数据创建动态表

[英]Create dynamic table with api data

How do I make my table dynamically list all the values it pulls from the api, atm it only pulls 1 record and puts it in the table, the rest of the data gets put outside it 我如何使我的表动态列出从api中提取的所有值,atm它仅提取1条记录并将其放入表中,其余数据置于其外部

Probably something very small I'm missing here, but can't put my finger on it 我可能在这里想念的东西很小,但不能把手指放在上面

Thanks for any help, very appreciated. 感谢您的帮助,非常感谢。

<?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
        }
     }
}
?>

Didnt close foreach in right place. 没有在正确的位置关闭foreach。 use this code instead 使用此代码

<?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

    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM