drcomwc134525 2017-03-11 15:32
浏览 8
已采纳

PHP数组到HTML表问题

I have my code working to some degree as it echo's out my array to a table - but the issue is, it's echoing out all fields.

I want to display just the name, trainer and status fields. I just can't get my head around how to do it.

Array

[{
id: "8001073",
name: "Castarnie",
silk: "c20170311her/00829186.jpg",
cloth_number: "4",
jockey: "Harry Cobden",
trainer: "Robert Walford",
weight_value: "165",
weight_unit: "pounds",
form: "P-2P2PF",
bred: "GB",
last_run: "21",
status: "WINNER"
},
{
id: "7164976",
name: "Try It Sometime",
silk: "c20170311her/00026136.jpg",
cloth_number: "8",
jockey: "Mikey Hamill",
trainer: "Sheila Lewis",
weight_value: "140",
weight_unit: "pounds",
form: "654529",
bred: "IRE",
last_run: "20",
status: "LOSER"
}
]

and the PHP code looks like

<?php if (count($obj) > 0): ?>
<table>
  <thead>
    <tr>
      <th><?php echo implode('</th><th>', array_keys(current($obj))); ?></th>
    </tr>
  </thead>
  <tbody>
<?php foreach ($obj as $row): array_map('htmlentities', $row); ?>
    <tr>
      <td><?php echo implode('</td><td>', $row); ?></td>
    </tr>
<?php endforeach; ?>
  </tbody>
</table>
<?php endif; ?>
  • 写回答

2条回答 默认 最新

  • dongxun7301 2017-12-01 23:47
    关注

    It doesn't make sense to call array_map() to modify every value in each row with htmlentities because you are only displaying three of them -- only modify what you are going to display.

    Code: (Demo)

    $json_data=file_get_contents($api_url);    
    $objects=json_decode($json_data);
    if(is_array($objects) && sizeof($objects)){
        echo '<table><thead><tr>';
            echo '<th>Name</th><th>Trainer</th><th>Status</th>';
        echo '</tr></thead><tbody>';
        foreach($objects as $row){
            echo '<tr>';
                echo '<td>',htmlentities($row->name),'</td>';
                echo '<td>',htmlentities($row->trainer),'</td>';
                echo '<td>',htmlentities($row->status),'</td>';
            echo '</tr>';
        }
        echo '</tbody></table>';
    }else{
        echo 'Invalid or empty json data received';
    }
    

    Output:

    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Trainer</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Castarnie</td>
                <td>Robert Walford</td>
                <td>WINNER</td>
            </tr>
            <tr>
                <td>Try It Sometime</td>
                <td>Sheila Lewis</td>
                <td>LOSER</td>
            </tr>
        </tbody>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接