Table 在 PHP 中使用平面文件数据库

Table in PHP with flatfile database

我正在尝试以 table 格式显示文本文件的内容,两列四行:

Mateo;Pérez
Marcos;Martínez
Lucas;Télez
Juan;Pez

这是我正在使用的PHP,但结果不是我想要的:

<?php

$course = file_get_contents("course.txt");
$line = explode("\n", $course);
for($i = 0; $i<count($line); $i++) {
    $item = explode(";", $line[$i]);
    {echo"

<table border='1' style='width:100%'>
  <tr>
    <td>".$item[0]."</td>
    <td>".$item[1]."</td>
  </tr>

</table>

";
    }
}
?>

这是我得到的:

这可能就是您想要的。

<?php

$course = file_get_contents("course.txt");
$line = explode("\n", $course);

echo "<table border='1' style='width:100%'>";

for($i = 0; $i<count($line); $i++) 
{
    $item = explode(";", $line[$i]);
    {
    echo "
    <tr>
        <td>".$item[0]."</td>
        <td>".$item[1]."</td>
    </tr>
    ";
    }
}

echo "</table>"

?>

您遇到的问题是您在循环 table 以及 TR 和 TD 标签。所以基本上你有多个 tables,而不是一个。

   <?php

    $course = file_get_contents("course.txt");
        $line = explode("\n", $course);
    ?>        
   <table border='1' style='width:100%'>
   <?php
    for($i = 0; $i<count($line); $i++) {
            $item = explode(";", $line[$i]);
            {echo"


      <tr>
        <td>".$item[0]."</td>
        <td>".$item[1]."</td>
      </tr>



    ";
            }
        }?>
   <?php
    </table>
    ?>
<table border='1' style='width:100%'>
<?php

$course = file_get_contents("course.txt");
    $line = explode("\n", $course);
    for($i = 0; $i<count($line); $i++) {
        $item = explode(";", $line[$i]);
        {echo"


  <tr>
    <td>".$item[0]."</td>
    <td>".$item[1]."</td>
  </tr>";
    }
}

?>
</table>

您的 html 结构不符合预期结果。也尝试制作清晰的代码..试试这个-

<table border='1' style='width:100%'>
<?php

$course = file_get_contents("course.txt");
    $line = explode("\n", $course);
    for($i = 0; $i<count($line); $i++) {
        $item = explode(";", $line[$i]);
        {
?>


  <tr>
    <td><?php echo $item[0]; ?></td>
    <td><?php echo $item[1]; ?></td>
  </tr>



<?php
        }
    }

?>
</table>