【问题标题】:Getting data from text file and display it in html table从文本文件中获取数据并在 html 表格中显示
【发布时间】:2015-07-05 02:25:59
【问题描述】:

我为每一行得到一个这种模式的文本文件:

Username : Score

我正在尝试以此创建一个记分牌。

这是我的尝试:

<table width="200" border="1">
  <tr>
    <td width="85">Nom</td>
    <td width="99">Score</td>
  </tr>
  <tr>
    <td height="119"></td>
    <td></td>
  </tr>
</table>

问题是如何将每个usernamescore 复制到表中(没有: 字符)?

编辑:

我当前的php代码:

<?php 

    $file = file_get_contents('facile.txt', true);
    $arr = explode("/", $file, 2);
    $first = $arr[0];

?>

这只会给我第一个用户名,但我想从每一行获取所有用户名。

【问题讨论】:

  • 你试过了吗?从文件中获取数据?
  • 我知道如何使用 php 从文件中获取数据,但我真的不知道如何在表格中用他的分数来组织每个用户名,我迷路了
  • ^ 然后展示你的努力和你所做的工作!将其包含在您的问题中!
  • 只需edit您的问题并添加您的尝试
  • 谢谢,我编辑了我的帖子

标签: php html file html-table


【解决方案1】:

这应该适合你:

在这里,我首先使用file() 将所有行放入一个数组中,其中每一行都是一个数组元素。在那里,我忽略了每行末尾的空行和换行符。

在此之后,我使用array_map() 遍历每一行并使用explode() 提取用户名+分数,然后我将其作为数组返回以创建一个多维数组,例如:

Array
(
    [0] => Array
        (
            [username] => a
            [score] => 5
        )
    //...

我使用usort() 按分数对数组进行排序(要将顺序从ASC 更改为DESC,您只需将usort() 中的&gt; 更改为&lt;),然后我只需循环遍历数据并打印在表格中。

<?php 

    $lines = file("scores.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $data = array_map(function($v){
        list($username, $score) = explode(":", $v);
        return ["username" => $username, "score" => $score];
    }, $lines);

    usort($data, function($a, $b){
        if($a["score"] == $b["score"])
            return 0;
        return $a["score"] > $b["score"] ? 1 : -1;
    });

?>

<table width="200" border="1">
    <tr>
        <td width="85">Nom</td>
        <td width="99">Score</td>
    </tr>
<?php foreach($data as $user){ ?>
    <tr>
        <td height="119"><?php echo $user["username"]; ?></td>
        <td><?php echo $user["score"]; ?></td>
    </tr>
<?php } ?>
</table>

输出:

Nom   Score  // Nom   Score
 e      2    //  d     123
 a      5    //  c     26
 b     15    //  b     15
 c     26    //  a      5
 d    123    //  e      2

【讨论】:

  • 更新:我对其进行了测试,它运行良好。谢谢
  • @SeifHassine 不客气! (所以我假设您实际上希望对表格进行排序?)
  • 啊,一开始我只是想直接将数据从文件传输到表,但是按分数对用户名进行排序总是更好,这就是我选择你的答案的原因
【解决方案2】:

示例文本文件数据

user1 : 100
user2 : 80
user3 : 60
user4 user4 : 75

PHP 代码

<table width="200" border="1">
    <tr>
        <td width="85">Nom</td>
        <td width="99">Score</td>
    </tr>
<?php
$file_handle = fopen("sample.txt", "rb");

while (!feof($file_handle) ) {
    $line_of_text = fgets($file_handle);
    $parts = explode(':', $line_of_text);
    echo "<tr><td height='119'>$parts[0]</td><td>$parts[1]</td></tr>";
}
fclose($file_handle);
?>
</table>

为了解决您在“:”之间分隔文本的问题,您可以使用爆炸功能。爆炸功能需要两个参数。第一个是要分割的字符(分隔符),第二个是要分割的字符串。像你这样的例子是文本文件。您必须逐行阅读文本文件。因此,您可以使用 fgets() 函数并分解该行。希望这能帮助你理解。

【讨论】:

  • @zer0fl4g 仅仅因为答案有帮助并不意味着 OP 必须接受您的答案(也许 upV 它,因为如果您将鼠标悬停在 upV 箭头上,工具提示也会这样说)!有一个很好的链接可以帮助决定 OP 可以/应该接受什么答案:meta.stackexchange.com/q/5234
【解决方案3】:

Rizier123's answer 非常完美。但我必须做一些改变才能让它在我的系统中工作。

<!DOCTYPE HTML>
<html>
<body>
<?php

    $lines = file("scores.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    function explodeLine($v)
    {
        list($username, $score) = explode(":", $v);
        return array("username" => $username, "score" => $score);
    }
    $data = array_map('explodeLine', $lines);

    function comparatorFunc($a, $b)
    {
        if($a["score"] == $b["score"])
            return 0;
        return $a["score"] > $b["score"] ? 1 : -1;
    }
    usort($data, 'comparatorFunc');

?>

<table width="200" border="1">
    <tr>
        <td width="85">Nom</td>
        <td width="99">Score</td>
    </tr>
<?php foreach($data as $user){ ?>
    <tr>
        <td height="119"><?php echo $user["username"]; ?></td>
        <td><?php echo $user["score"]; ?></td>
    </tr>
<?php } ?>
</table>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2012-05-27
    • 1970-01-01
    相关资源
    最近更新 更多