• <legend id='7HgtP'><style id='7HgtP'><dir id='7HgtP'><q id='7HgtP'></q></dir></style></legend>
      • <bdo id='7HgtP'></bdo><ul id='7HgtP'></ul>

        <small id='7HgtP'></small><noframes id='7HgtP'>

        <i id='7HgtP'><tr id='7HgtP'><dt id='7HgtP'><q id='7HgtP'><span id='7HgtP'><b id='7HgtP'><form id='7HgtP'><ins id='7HgtP'></ins><ul id='7HgtP'></ul><sub id='7HgtP'></sub></form><legend id='7HgtP'></legend><bdo id='7HgtP'><pre id='7HgtP'><center id='7HgtP'></center></pre></bdo></b><th id='7HgtP'></th></span></q></dt></tr></i><div id='7HgtP'><tfoot id='7HgtP'></tfoot><dl id='7HgtP'><fieldset id='7HgtP'></fieldset></dl></div>
        <tfoot id='7HgtP'></tfoot>

        从文本文件中获取数据并将其显示在 html 表中

        时间:2023-10-04

          <bdo id='MPDFB'></bdo><ul id='MPDFB'></ul>
          <legend id='MPDFB'><style id='MPDFB'><dir id='MPDFB'><q id='MPDFB'></q></dir></style></legend>
        • <tfoot id='MPDFB'></tfoot>
          <i id='MPDFB'><tr id='MPDFB'><dt id='MPDFB'><q id='MPDFB'><span id='MPDFB'><b id='MPDFB'><form id='MPDFB'><ins id='MPDFB'></ins><ul id='MPDFB'></ul><sub id='MPDFB'></sub></form><legend id='MPDFB'></legend><bdo id='MPDFB'><pre id='MPDFB'><center id='MPDFB'></center></pre></bdo></b><th id='MPDFB'></th></span></q></dt></tr></i><div id='MPDFB'><tfoot id='MPDFB'></tfoot><dl id='MPDFB'><fieldset id='MPDFB'></fieldset></dl></div>
              <tbody id='MPDFB'></tbody>

            <small id='MPDFB'></small><noframes id='MPDFB'>

                  本文介绍了从文本文件中获取数据并将其显示在 html 表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I got a text file in this pattern for each line:

                  Username : Score
                  

                  我正在尝试用它创建一个记分牌.

                  I'm trying to create a scoreboard out of this.

                  这是我的尝试:

                  <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 复制到表中(没有 : 字符)?

                  The question is how can I copy each username and score to the table (without the : character) ?

                  我当前的 php 代码:

                  My current php code:

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

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

                  This will give me only the first username, but I want to get all the usernames from every line.

                  推荐答案

                  这应该适合你:

                  这里我首先使用 file() 其中每一行都是一个数组元素.在那里我忽略每行末尾的空行和换行符.

                  Here I first get all lines into an array with file() where every line is one array element. There I ignore empty lines and new line characters at the end of each line.

                  在此之后,我使用 array_map()并使用explode(),然后我将其作为数组返回以创建一个多维数组,例如:

                  After this I go through each line with array_map() and extract the username + score with explode(), which I then return as array to create a multidimensional array, e.g:

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

                  我用 usort()(要将顺序从 ASC 更改为 DESC,您只需将 > 更改为 <> 在 usort()) 之后,我简单地遍历数据并将其打印在表格中.

                  The I sort the array by the score with usort() (To change the order from ASC to DESC you can just change > to < in usort()) and after this I simply loop through the data and print it in the table.

                  <?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
                  

                  这篇关于从文本文件中获取数据并将其显示在 html 表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                    <tbody id='TWvPz'></tbody>

                  • <bdo id='TWvPz'></bdo><ul id='TWvPz'></ul>

                    <small id='TWvPz'></small><noframes id='TWvPz'>

                    <legend id='TWvPz'><style id='TWvPz'><dir id='TWvPz'><q id='TWvPz'></q></dir></style></legend>
                    <i id='TWvPz'><tr id='TWvPz'><dt id='TWvPz'><q id='TWvPz'><span id='TWvPz'><b id='TWvPz'><form id='TWvPz'><ins id='TWvPz'></ins><ul id='TWvPz'></ul><sub id='TWvPz'></sub></form><legend id='TWvPz'></legend><bdo id='TWvPz'><pre id='TWvPz'><center id='TWvPz'></center></pre></bdo></b><th id='TWvPz'></th></span></q></dt></tr></i><div id='TWvPz'><tfoot id='TWvPz'></tfoot><dl id='TWvPz'><fieldset id='TWvPz'></fieldset></dl></div>

                        <tfoot id='TWvPz'></tfoot>