从文本文件获取数据并显示在 html table
Getting data from text file and display it in html table
我得到了一个文本文件,每一行都采用这种模式:
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>
问题是如何将每个 username
和 score
复制到 table(没有 :
字符)?
编辑:
我目前的php代码:
<?php
$file = file_get_contents('facile.txt', true);
$arr = explode("/", $file, 2);
$first = $arr[0];
?>
这只会给我第一个用户名,但我想从每一行中获取所有用户名。
这应该适合你:
在这里,我首先将所有行放入一个 file()
的数组中,其中每一行都是一个数组元素。在那里我忽略了每行末尾的空行和换行符。
在此之后,我用 array_map()
and extract the username + score with explode()
遍历每一行,然后我将其 return 作为数组来创建一个多维数组,例如:
Array
(
[0] => Array
(
[username] => a
[score] => 5
)
//...
我使用 usort()
按分数对数组进行排序(要将顺序从 ASC
更改为 DESC
,您只需将 >
更改为 <
在 usort()
) 之后,我简单地遍历数据并将其打印在 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
示例文本文件数据
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>
要解决您在“:”之间分隔文本的问题,您可以使用爆炸功能。 explode 函数需要两个参数。第一个是你要分隔的字符(定界符),第二个是你要分隔的字符串。像你这样的例子是文本文件。您必须逐行阅读文本文件。因此,您可以使用 fgets() 函数并展开该行。希望这会帮助你理解。
非常完美。但是我必须进行一些更改才能使其在我的系统中运行。
<!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>
我得到了一个文本文件,每一行都采用这种模式:
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>
问题是如何将每个 username
和 score
复制到 table(没有 :
字符)?
编辑:
我目前的php代码:
<?php
$file = file_get_contents('facile.txt', true);
$arr = explode("/", $file, 2);
$first = $arr[0];
?>
这只会给我第一个用户名,但我想从每一行中获取所有用户名。
这应该适合你:
在这里,我首先将所有行放入一个 file()
的数组中,其中每一行都是一个数组元素。在那里我忽略了每行末尾的空行和换行符。
在此之后,我用 array_map()
and extract the username + score with explode()
遍历每一行,然后我将其 return 作为数组来创建一个多维数组,例如:
Array
(
[0] => Array
(
[username] => a
[score] => 5
)
//...
我使用 usort()
按分数对数组进行排序(要将顺序从 ASC
更改为 DESC
,您只需将 >
更改为 <
在 usort()
) 之后,我简单地遍历数据并将其打印在 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
示例文本文件数据
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>
要解决您在“:”之间分隔文本的问题,您可以使用爆炸功能。 explode 函数需要两个参数。第一个是你要分隔的字符(定界符),第二个是你要分隔的字符串。像你这样的例子是文本文件。您必须逐行阅读文本文件。因此,您可以使用 fgets() 函数并展开该行。希望这会帮助你理解。
<!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>