如何使用php将文本文件读入HTML表?
我有一个正在读取的文本文件。主要目标是将文本文件中的信息显示到html表中。我的信息读取正常,但是所有信息都显示在同一行中 当前代码: 表格标题如何使用php将文本文件读入HTML表?,php,html,Php,Html,我有一个正在读取的文本文件。主要目标是将文本文件中的信息显示到html表中。我的信息读取正常,但是所有信息都显示在同一行中 当前代码: 表格标题 <tr> <th>Title</th> <th>First Name</th> <th>Last Name</th> <th>Date of Bi
<tr>
<th>Title</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date of Birth</th>
<th>Social Number</th>
<th>Address</th>
<th>Email</th>
<th>Phone Number</th>
</tr>
<?php
if(!file_exists("patientdata.txt"))
{
echo "The file from above cannot be found!";
exit;
}
$fp = fopen("patientdata.txt", "r");
if(!$fp)
{
echo "File cannot be opened";
exit;
}
echo "<table border = 3>";
while(!feof($fp))
{
$info = fgets($fp);
echo "<td>$info</td>\n";
}
echo "</td>\n";
echo "</table>";
fclose($fp)
?>
</body>
标题
名字
姓
出生日期
社会号码
地址
电子邮件
电话号码
您可以使用以下命令在HTML表格中打印文本文件中的数据:
注:
..
是一个小型的内嵌式文件,允许您设置表格和页面中其他HTML元素的样式(您还可以使用所有文档样式创建一个单独的css文件,并将其包含在HTML文档的..
中)
要从数据创建HTML表,需要确保不超过列数(本例中为8),因此我们将$cols
设置为8
,并使用计数器($count
)跟踪
因此,当8个列
被数据填充时,一个新的行
将启动。逻辑会一直呈现行,直到没有更多的数据,并且表被关闭
<?php
if (!file_exists("patientdata.txt")) {
echo "The file from above cannot be found!";
exit;
}
$fp = fopen("patientdata.txt", "r");
if (!$fp) {
echo "File cannot be opened";
exit;
}
// a bit of styling...
echo <<<EOF
<style>
table, td, th {
table-layout: fixed;
width: 100%;
border-collapse: collapse;
border: 3px solid black;
text-align: left;
}
</style>
EOF;
$count = 0;
$cols = 8; // the number of data items per row
echo '<table>'; // open table
// render headers
echo '<tr><th>Title</th><th>First Name</th><th>Last Name</th>
<th>Date of Birth</th><th>Social Number</th><th>Address</th>
<th>Email</th><th>Phone Number</th></tr>';
echo '<tr>'; // open first row
while(!feof($fp))
{
if($count < $cols) {
$info = fgets($fp);
echo "<td>$info</td>"; // render data item
$count++;
} else {
$count = 0; // reset counter
echo '</tr><tr>'; // close current row, start new row
}
}
echo "</tr></table>"; // close final row, close table
fclose($fp); // close file handle
使用文本文件中的数据是如何分隔的?文件内容逐行保存,因此输入的每个变量都保存到新行。乔尔·罗伯茨小姐18-02-2020 9082179041等等,我可以帮忙@如果这回答了你的问题,你应该按一下按钮接受这个答案,也可以考虑投票。