如何在数据表中回显JSON数据?

时间:2018-03-08 10:08:10

标签: php json pdo datatable

我在名为 json.php 的php中有一个Json数据,现在我需要回显我的数据表中的每个数据,其中包含 index.php

我的问题是:

  1. 从另一个php文件传递json数据的最佳做法是什么?

  2. 之后,如何回显每个数据?

  3. 这是 json.php ,php文件生成我的查询到编码的JSON:

    <?php
    include "connect2.php";
    $select = $con->prepare("SELECT id, dateApply, school, city, name, xType, xLevel, phone, status, lastUpdated FROM t_applicants WHERE dateApply between '2018-01-01' AND '2018-12-31'");
    $select->execute();
    
    $data = array();
    
        while($row=$select->fetch(PDO::FETCH_ASSOC)){
            $data['data'][] = $row;
        }
        echo json_encode($data);
    ?>
    

    index.php &gt; 这是正文数据库代码:

    <tbody>
       <?php
          //How to pass json.php to this code?
          //how to fetch the data from json.php?
          {?>
       <tr>
          <td><?php echo $data['id']; ?></td>
          <td><?php
             echo $apply = (new DateTime($data['dateApply']))->format('Y-m-d');
             ?></td>
          <td><?php echo $data['name']; ?></td>
          <td><?php echo $data['school']; ?></td>
          <td><?php echo $data['city']; ?></td>
          <td><?php echo $data['status']; ?></td>
          <td>
             <?php
                $timestamp = strtotime($data['lastUpdated']);
                $date = date('Y-m-d H:i:s', $timestamp);
                echo $date;
                ?>
          </td>
          <td>
             <button type="button" data-a="<?php echo $data['id'];?>" data-b="<?php echo $yearX; ?>" href='#detailUpdate' class="modalDetailUpdate btn btn-primary btn-xs" data-toggle="modal" data-backdrop='static' data-keyboard='false' title='Editar usuario'><span class="glyphicon glyphicon-edit"></span></button>
          </td>
          <?php
             }?>
       </tr>
    </tbody>
    

    感谢您的帮助。我真的很欣赏它......

1 个答案:

答案 0 :(得分:1)

<?php
$json = json_decode($data, true);
foreach($json['data'] as $row) { ?>
  <td><?php echo $row['id']; ?></td>
   ......
<?php } ?>