为foreach php提供的参数错误

时间:2014-03-02 14:18:59

标签: php json

我有这个代码在Chrome浏览器上显示一个错误。

<?php
    $json = file_get_contents("file.json");
    $data =  json_decode($json, TRUE);


?>
<html>
<?php include 'cab.php'; ?>
<body>
    <?php include 'menu.php'; ?>

    <div class="container">
        <table border="1" class="table table-striped table-hover" id="tbl">
            <tr>
                <td><strong>Data de publicação</strong></td>
                <td><strong>Empresa Contratada</strong></td>
                <td><strong>Empresa que Contratou</strong></td>
                <td><strong>ID</strong></td>
                <td><strong>Objecto adquirido</strong></td>
                <td><strong>Preço Contratual</strong></td>
                <td><strong>Data do Contrato</strong></td>
            </tr>
            <?php foreach ($data as $item) : ?>
                <tr>
                    <td><?php echo $item->{'publicationDate'}; ?></td>
                    <td><?php echo $item->{'contracted'}; ?></td>
                    <td><?php echo $item->{'contracting'}; ?></td>
                    <td><?php echo $item->{'id'}; ?></td>
                    <td><?php echo $item->{'objectBriefDescription'}; ?></td>
                    <td><?php echo $item->{'initialContractualPrice'}; ?></td>
                    <td><?php echo $item->{'signingDate'}; ?></td>
                </tr>
            <?php endforeach; ?>
        </table>
    </div>
    <?php include 'rodape.php';?>
</body>
</html>

Chrome上显示的错误是:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\base2\index.php on line 27

有人可以帮我解决这个问题吗?

此文件reeds一个json文件并在html表中显示所有信息。

1 个答案:

答案 0 :(得分:0)

<div class="container">
<table border="1" class="table table-striped table-hover" id="tbl">
    <tr>
        <td><strong>Data de publicação</strong></td>
        <td><strong>Empresa Contratada</strong></td>
        <td><strong>Empresa que Contratou</strong></td>
        <td><strong>ID</strong></td>
        <td><strong>Objecto adquirido</strong></td>
        <td><strong>Preço Contratual</strong></td>
        <td><strong>Data do Contrato</strong></td>
    </tr>
    <?php 
      $json = file_get_contents("file.json");
      $data =  json_decode($json);
      if(count((array)$data)>0):
      foreach ($data as $item) : ?>
        <tr>
            <td><?php echo $item->publicationDate; ?></td>
            <td><?php echo $item->contracted; ?></td>
            <td><?php echo $item->contracting; ?></td>
            <td><?php echo $item->id; ?></td>
            <td><?php echo $item->objectBriefDescription; ?></td>
            <td><?php echo $item->initialContractualPrice; ?></td>
            <td><?php echo $item->signingDate; ?></td>
        </tr>
    <?php endforeach; else: echo "No data found"; endif; ?>