用PHP对文本文件进行分页

用PHP对文本文件进行分页,php,file,text,pagination,flat,Php,File,Text,Pagination,Flat,我有一个显示文本文件中博客文章的小脚本,如何添加分页,使其一次只显示5篇博客文章 以下是脚本: <html> <head> <title>blog</title> </head> <body> <?php $mode = 0; if ($mode == 0) { $opFile = "blogfile.txt"; } $fp = fopen($opFile,"r") or die("Error R

我有一个显示文本文件中博客文章的小脚本,如何添加分页,使其一次只显示5篇博客文章

以下是脚本:

<html> 
<head>

<title>blog</title> 
</head> 
<body> 

<?php 

$mode = 0; 
if ($mode == 0) { $opFile = "blogfile.txt"; } 

$fp = fopen($opFile,"r") or die("Error Reading File"); 
  $data = fread($fp, filesize($opFile)); 
fclose($fp); 

$line = explode("\n", $data); 
$i=count($line); 

for ($n=0 ; $n < $i-1 ; $n++ ) { 
  $blog = explode("|", $line[$n]); 

  if (isset($blog[0])) 
   {     
    echo "<div class=\"blog-post\">";
    echo "<p class=\"blog-title\">".$blog[1]."</p>";         
    echo "<p class=\"blog-message\">".$blog[2]."</p>"; 
    echo "<p class=\"blog-date\">Posted: " .$blog[0]."</p>";
    echo "<div style=\"clear: both;\"></div>";
    echo "</div>";

 } 
 }        

?> 
</body> 
</html>
非常感谢您的帮助

类似这样:

<html> 
<head>

<title>blog</title> 
</head> 
<body> 

<?php 
$POSTS_PER_PAGE = 10;

//Not sure what this is for, but I left it?
$mode = 0;
if ($mode == 0) { $opFile = "blogfile.txt"; } 

//Explode the textfile into lines
$lines = file($opFile); 

$posts = array();
foreach($lines as $line) {
    //Ignore blank lines
    if($line != "") {
        //Explode each non-empty line
        $post = explode("|", $line);

        //Store the blog post
        array_push($posts, $post)
    }
}

//Output the pagination links
echo "<div class=\"blog-pagination\">";
for($i = 1; $i < ceil(count($posts) / $POSTS_PER_PAGE; $i++) {
    echo '<a href="http://mydomain/blog.php?page=' + $i + '">' + $i + '</a>&nbsp;';
}
echo "</div>";

//Assume the user wants the first page if it's not specified
if(!isset($_GET['page'])) {
    $_GET['page'] = 1;
}

//Figure out the first and last posts on this page
$first_post = ($_GET['page'] - 1) * $POSTS_PER_PAGE;
$last_post = $_GET['page'] * $POSTS_PER_PAGE - 1;

//Display the requested posts
for($i = $first_post; $i <= $last_post; $i++) {
    echo "<div class=\"blog-post\">";
    echo "<p class=\"blog-title\">".$blog[1]."</p>";         
    echo "<p class=\"blog-message\">".$blog[2]."</p>"; 
    echo "<p class=\"blog-date\">Posted: " .$blog[0]."</p>";
    echo "<div style=\"clear: both;\"></div>";
    echo "</div>";
}
?>

博客
大概是这样的:

<html> 
<head>

<title>blog</title> 
</head> 
<body> 

<?php 
$POSTS_PER_PAGE = 10;

//Not sure what this is for, but I left it?
$mode = 0;
if ($mode == 0) { $opFile = "blogfile.txt"; } 

//Explode the textfile into lines
$lines = file($opFile); 

$posts = array();
foreach($lines as $line) {
    //Ignore blank lines
    if($line != "") {
        //Explode each non-empty line
        $post = explode("|", $line);

        //Store the blog post
        array_push($posts, $post)
    }
}

//Output the pagination links
echo "<div class=\"blog-pagination\">";
for($i = 1; $i < ceil(count($posts) / $POSTS_PER_PAGE; $i++) {
    echo '<a href="http://mydomain/blog.php?page=' + $i + '">' + $i + '</a>&nbsp;';
}
echo "</div>";

//Assume the user wants the first page if it's not specified
if(!isset($_GET['page'])) {
    $_GET['page'] = 1;
}

//Figure out the first and last posts on this page
$first_post = ($_GET['page'] - 1) * $POSTS_PER_PAGE;
$last_post = $_GET['page'] * $POSTS_PER_PAGE - 1;

//Display the requested posts
for($i = $first_post; $i <= $last_post; $i++) {
    echo "<div class=\"blog-post\">";
    echo "<p class=\"blog-title\">".$blog[1]."</p>";         
    echo "<p class=\"blog-message\">".$blog[2]."</p>"; 
    echo "<p class=\"blog-date\">Posted: " .$blog[0]."</p>";
    echo "<div style=\"clear: both;\"></div>";
    echo "</div>";
}
?>

博客

这在我的测试中起了作用:

define('MAX_PER_PAGE',10);
// sanity checks for per-page and page index
$numPosts = ctype_digit((string)$_GET['perpage']) ? $_GET['perpage'] : 5;
$ostart = $start = max(1, ctype_digit((string)$_GET['page']) ? $_GET['page'] : 1) - 1;

$mode = 0; 
if ($mode == 0) { 
    $file = "blogfile.txt";
}

// read the file into an array, strip newlines and ignore empty lines
file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES | FILE_TEXT);

// sort array (see custom function at bottom)
usort($line, 'blogsort');

$lines = count($line);

// get total number of pages
$numPages = ceil($lines / $numPosts);

// additional sanity checks (also sets $ostart if it was invalid; used later)
$numPosts = min(MAX_PER_PAGE, max(1, $numPosts));
if ($start * $numPosts > $lines ) {
    $ostart = $start = max(0, $lines - $numPosts);
}
else {
    $start *= $numPosts;
}

// Only grab the part of the array we need
$sliced = array_slice($line, $start, $numPosts);

// loop through posts, but break early if we run out
for ($n = 0; $n < $numPosts && isset($sliced[$n]); $n++ ) {
    $blog = explode("|", $sliced[$n]); 

    if (isset($blog[0])) {     
        echo "<div class=\"blog-post\">\n",
             "<p class=\"blog-title\">{$blog[1]}</p>\n",
             "<p class=\"blog-message\">{$blog[2]}</p>\n",
             "<p class=\"blog-date\">Posted: {$blog[0]}</p>\n",
             "<div style=\"clear: both;\"></div>\n",
             "</div>\n\n";

    }
}
// back link
if ($ostart > 0) {
    echo "<a href=\"{$_SERVER['SCRIPT_NAME']}?perpage={$numPosts}&page={$ostart}\">&larr; Older</a>";
}
else {
    echo "None Older";
}
echo " || ";
// forward link
if ($ostart + 1 < $numPages) {
    $next = $ostart + 2;
    echo "<a href=\"{$_SERVER['SCRIPT_NAME']}?perpage={$numPosts}&page={$next}\">Newer &rarr;</a>";
}
else {
    echo "None Newer";
}

function blogsort($a, $b) {
    $dateA = strtotime(substr($a, 0, strpos($a, '|')));
    $dateB = strtotime(substr($b, 0, strpos($b, '|')));

    if ($dateA == $dateB) {
        return 0;
    }
    elseif ($dateA > $dateB) {
        return -1;
    }
    else {
        return 1;
    }
}
define('MAX_PER_PAGE',10);
//每页和页索引的完整性检查
$numPosts=ctype\u digit((字符串)$\u GET['perpage'])$_获取['perpage']:5;
$ostart=$start=max(1,ctype_数字((字符串)$_GET['page'])?$_GET['page']:1)-1;
$mode=0;
如果($mode==0){
$file=“blogfile.txt”;
}
//将文件读入数组,去掉换行符并忽略空行
文件($file,file_IGNORE_NEW|line | file_SKIP_EMPTY|line | file_TEXT);
//排序数组(请参见底部的自定义函数)
usort($line,'blogsort');
$line=计数($line);
//获取总页数
$numPages=ceil($line/$numPosts);
//附加的健全性检查(如果$ostart无效,也设置$ostart;稍后使用)
$numPosts=min(每页最大,最大(1,$numPosts));
如果($start*$numPosts>$lines){
$ostart=$start=max(0,$lines-$numPosts);
}
否则{
$start*=$numPosts;
}
//只抓取阵列中我们需要的部分
$sliced=array\u slice($line,$start,$numPosts);
//循环浏览帖子,但如果用完了,就早点休息
对于($n=0;$n<$numPosts&&isset($sliced[$n]);$n++){
$blog=explode(“|”,$sliced[$n]);
if(isset($blog[0]){
回显“\n”,
“

{$blog[1]}

\n”, “

{$blog[2]}

\n”, “

发布:{$blog[0]}

\n”, “\n”, “\n\n”; } } //反向链接 如果($ostart>0){ 回声“; } 否则{ 呼应“不老”; } 回声“| |”; //前向链路 如果($ostart+1<$numPages){ $next=$ostart+2; 回声“; } 否则{ 呼应“无更新”; } 函数blogsort($a,$b){ $dateA=strotime(substr($a,0,strpos($a,“|”)); $dateB=strottime(substr($b,0,strpos($b,“|”)); 如果($dateA==$dateB){ 返回0; } elseif($dateA>$dateB){ 返回-1; } 否则{ 返回1; } }
这在我的测试中起作用:

define('MAX_PER_PAGE',10);
// sanity checks for per-page and page index
$numPosts = ctype_digit((string)$_GET['perpage']) ? $_GET['perpage'] : 5;
$ostart = $start = max(1, ctype_digit((string)$_GET['page']) ? $_GET['page'] : 1) - 1;

$mode = 0; 
if ($mode == 0) { 
    $file = "blogfile.txt";
}

// read the file into an array, strip newlines and ignore empty lines
file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES | FILE_TEXT);

// sort array (see custom function at bottom)
usort($line, 'blogsort');

$lines = count($line);

// get total number of pages
$numPages = ceil($lines / $numPosts);

// additional sanity checks (also sets $ostart if it was invalid; used later)
$numPosts = min(MAX_PER_PAGE, max(1, $numPosts));
if ($start * $numPosts > $lines ) {
    $ostart = $start = max(0, $lines - $numPosts);
}
else {
    $start *= $numPosts;
}

// Only grab the part of the array we need
$sliced = array_slice($line, $start, $numPosts);

// loop through posts, but break early if we run out
for ($n = 0; $n < $numPosts && isset($sliced[$n]); $n++ ) {
    $blog = explode("|", $sliced[$n]); 

    if (isset($blog[0])) {     
        echo "<div class=\"blog-post\">\n",
             "<p class=\"blog-title\">{$blog[1]}</p>\n",
             "<p class=\"blog-message\">{$blog[2]}</p>\n",
             "<p class=\"blog-date\">Posted: {$blog[0]}</p>\n",
             "<div style=\"clear: both;\"></div>\n",
             "</div>\n\n";

    }
}
// back link
if ($ostart > 0) {
    echo "<a href=\"{$_SERVER['SCRIPT_NAME']}?perpage={$numPosts}&page={$ostart}\">&larr; Older</a>";
}
else {
    echo "None Older";
}
echo " || ";
// forward link
if ($ostart + 1 < $numPages) {
    $next = $ostart + 2;
    echo "<a href=\"{$_SERVER['SCRIPT_NAME']}?perpage={$numPosts}&page={$next}\">Newer &rarr;</a>";
}
else {
    echo "None Newer";
}

function blogsort($a, $b) {
    $dateA = strtotime(substr($a, 0, strpos($a, '|')));
    $dateB = strtotime(substr($b, 0, strpos($b, '|')));

    if ($dateA == $dateB) {
        return 0;
    }
    elseif ($dateA > $dateB) {
        return -1;
    }
    else {
        return 1;
    }
}
define('MAX_PER_PAGE',10);
//每页和页索引的完整性检查
$numPosts=ctype\u digit((字符串)$\u GET['perpage'])$_获取['perpage']:5;
$ostart=$start=max(1,ctype_数字((字符串)$_GET['page'])?$_GET['page']:1)-1;
$mode=0;
如果($mode==0){
$file=“blogfile.txt”;
}
//将文件读入数组,去掉换行符并忽略空行
文件($file,file_IGNORE_NEW|line | file_SKIP_EMPTY|line | file_TEXT);
//排序数组(请参见底部的自定义函数)
usort($line,'blogsort');
$line=计数($line);
//获取总页数
$numPages=ceil($line/$numPosts);
//附加的健全性检查(如果$ostart无效,也设置$ostart;稍后使用)
$numPosts=min(每页最大,最大(1,$numPosts));
如果($start*$numPosts>$lines){
$ostart=$start=max(0,$lines-$numPosts);
}
否则{
$start*=$numPosts;
}
//只抓取阵列中我们需要的部分
$sliced=array\u slice($line,$start,$numPosts);
//循环浏览帖子,但如果用完了,就早点休息
对于($n=0;$n<$numPosts&&isset($sliced[$n]);$n++){
$blog=explode(“|”,$sliced[$n]);
if(isset($blog[0]){
回显“\n”,
“

{$blog[1]}

\n”, “

{$blog[2]}

\n”, “

发布:{$blog[0]}

\n”, “\n”, “\n\n”; } } //反向链接 如果($ostart>0){ 回声“; } 否则{ 呼应“不老”; } 回声“| |”; //前向链路 如果($ostart+1<$numPages){ $next=$ostart+2; 回声“; } 否则{ 呼应“无更新”; } 函数blogsort($a,$b){ $dateA=strotime(substr($a,0,strpos($a,“|”)); $dateB=strottime(substr($b,0,strpos($b,“|”)); 如果($dateA==$dateB){ 返回0; } elseif($dateA>$dateB){ 返回-1; } 否则{ 返回1; } }
为什么要从文本文件而不是数据库中读取?因为它是不使用任何数据库的较大脚本的一部分,这是它的“功能”之一。只要把它放到服务器上就可以了。我知道DB更好,但这就是我需要它的工作方式。为什么你要从文本文件而不是数据库中读取?因为它是不使用任何数据库的较大脚本的一部分,这是它的“功能”之一。只要把它放到服务器上就可以了。我知道DB更好,但这就是我需要它的工作方式。好吧,你救赎了自己。谢谢你的帮助,很抱歉说你傲慢;)我会尝试一下,看看会怎么样。再次感谢!没问题,请检查进度/问题!(更新了上面的代码)检查file()函数将文件读取到数组中:如果使用PHP5,可以添加以下标志:(file_IGNORE_NEW_LINES | file_SKIP_EMPTY_LINES)以删除更多代码OK,您重新获得了自我。谢谢你的帮助,很抱歉说你傲慢;)我会尝试一下,看看会怎么样。再次感谢!没问题,请检查进度/问题!(上面更新了我的代码)查看file()函数将文件读取到数组中:如果使用PHP5,可以添加标志:(file_IGNORE_NEW_line | file_SKIP_EMPTY_line)以消除更多的代码非常感谢,这很好,但是如果有7个条目,它将只显示5个。还有,我该如何让最新的文章排在第一位呢?我的意思是,除非还有5篇文章,否则不会有下一集的链接。理想情况下,即使只有一篇文章,也会有一个链接。我还更新了脚本,以按日期对博客实体进行排序,并首先显示最新的内容。我再次更新了脚本,以采纳Dolph的想法,并使用
file()
函数,因为它比我们所能实现的任何功能都更快、更好