如何在显示文本文件内容时设置页码

how can i set page number in showing contents of text file

我有文本文件,我想显示页码以便在页面中显示
例如我的文本文件有 100 行,我可以用 table 中的数组显示它们,一行中的每一行如下所示:
这是我的文本文件示例:

mycontent-54564-yoursdsd
condsadtent-5544564-fyfdfdsd
....
//convert into array
$lines = file('myfile.txt');
//count number of lines
$numbers=count($lines);
//show every line in a row of table :
foreach ($lines as $line) {
      $line=explode("-", $line);
      echo "<tr>";
      echo "<td>$line[0]</td>";
      echo "<td>$line[1]</td>";
      echo "<td>$line[2]</td>";
      echo "</tr>";
}

我想用 PHP 分页来查看这个 table ,例如在一页中显示每 10 行文本文件,第 1 页中的第 1 到 10 行,第 11 到 20 行在第 2 页等...我该怎么做?
谢谢

您可以使用 array_chunk (documentation);

array Chunks an array into arrays with size elements. The last chunk may contain less than size elements.

然后您更改代码:

$pages = array_chunk($lines, 10); // create chunk of 10 fr each page

foreach($pages as $i => $page) {
    echo "This is page num $i" . PHP_EOL;

    foreach ($page as $line) { // for each line in page
        $line=explode("-", $line);
        echo "<tr><td>$line[0]</td><td>$line[1]</td><td>$line[2]</td></tr>";
    }
}

已编辑:

如果您只想加载特定页面的数据,请执行以下操作:

$pageNum = _Session["page"]; // of whatever way you use to get page num - you may want to do -1 as array index starting from 0 - up to you
$pages = array_chunk(file('myfile.txt'), 10);

foreach ($pages[$pageNum] as $line) { // for each line in page you want
    $line=explode("-", $line);
    echo "<tr><td>$line[0]</td><td>$line[1]</td><td>$line[2]</td></tr>";
}