【问题标题】:Build HTML dynamic table from .txt file using PHP使用 PHP 从 .txt 文件构建 HTML 动态表
【发布时间】:2020-09-12 17:25:03
【问题描述】:

我正在尝试从定期更新的 .txt 文件 创建一个 html 动态表。 .txt 数据结构是这样的:

25/07/2020 13:45:22;1
25/07/2020 14:15:22;0
25/07/2020 17:44:20;1

html 表本身应该至少有 4 列(其中两列是硬编码的): 身份证 | 姓名 | 时间(来自.txt)| 状态(来自 .txt)

我怎样才能使用 PHP 来完成这项工作? 我无法使用 fopen

谢谢!

【问题讨论】:

  • 你可以使用 file_get_contents 吗?
  • 是的@DCR。这就是我目前正在尝试的。

标签: php html text dynamic html-table


【解决方案1】:

我会用三个独立的部分来做:

  1. 用户端的文件。

纯 HTML 和 JS - 您只需通过 jQuery ajax 连接到服务器上的 php 文件即可检索 .txt 文件的内容:

let yourData;
function waitForMsg(){
//get your Data from the Server
    $.ajax({
        type: "GET",
        url: "getData.php",

        async: true, 
        cache: false,
        timeout:50000,

        success: function(data){ 
            yourData = (data);
            renderStuff(); 
            ractive.update();
            setTimeout(
                waitForMsg,
                15000
            );
        }
    });

};
  1. 服务器上的文件

在您的服务器上,您在 getData.php 中获取数据:

<?php
$lines = file_get_contents("myData.txt"); //gives you the Data as Array for easier handling
echo ($lines);
?>
  1. 更新用户视图中的数据

我会使用 ractive.js 来呈现 html 中的数据而不重新加载它,但可能还有其他十几种方法:

function renderStuff() {
    const template = "{{#Data}}<tr><td>harcoded</td><td>harcoded</td><td>{{date}}</td>{{time}}</td></tr>{{/Data}}";
    ractive = new Ractive({
        el: '#target',
        template: template,
        modifyArrays: true,
        data: {yourData},
        computed: {
            Data() {
                let tempdoc = this.get('kinderchen');
                for (let x of tempdoc) {
                    //do something with your Data to make it more readable. I'd assume building a JSON out of it
                    //so it's something like: [{'date':'12/12/12'}, {'time':'15:22'}, ...]
                }

                return tempdoc;
            }
        }
    });
}

当然,这是一个非常简短的示例,需要进行一些更改才能工作,但这是一种开始方式。

【讨论】:

    猜你喜欢
    • 2013-03-10
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    相关资源
    最近更新 更多