【问题标题】:Table in PHP with flatfile database带有平面文件数据库的 PHP 表
【发布时间】:2015-12-24 04:26:08
【问题描述】:

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

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>

";
    }
}
?>

这是我得到的:

【问题讨论】:

  • 一定很喜欢下面有 10 个相同的答案。
  • 有人能告诉我我到底应该如何删除实际上是空的最后一行???我不知道为什么会出现。
  • 放入 for($i = 0; i &lt; count($line) - 1; $i++) 而不是 for($i = 0; $i&lt;count($line); $i++),因为数组大小是 3,所以你的 for 循环将执行 4 次(0、1、2、3),而不是 3它需要的次数 (0, 1, 2)

标签: php html html-table flat-file


【解决方案1】:

这可能是你想要的。

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

?>

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

【讨论】:

    【解决方案2】:
       <?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>
        ?>
    

    【讨论】:

      【解决方案3】:
      <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>
      

      【讨论】:

        【解决方案4】:

        您的 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>
        

        【讨论】:

          猜你喜欢
          • 2016-05-03
          • 2010-12-10
          • 1970-01-01
          • 2011-01-22
          • 1970-01-01
          • 1970-01-01
          • 2011-08-23
          • 2012-03-10
          • 1970-01-01
          相关资源
          最近更新 更多