使用 PHP 对文本文件进行分页

发布于 2024-08-22 03:28:23 字数 1169 浏览 10 评论 0原文

我有一个小脚本,用于显示文本文件中的博客文章,如何添加分页以便一次只显示 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>

这是文本文件:

Feb 17 2010|Title|Blog post content here|[end]
Feb 17 2010|Title|Blog post content here|[end]
Feb 17 2010|Title|Blog post content here|[end]
Feb 17 2010|Title|Blog post content here|[end]

非常感谢任何帮助!

I have a small script that displays blog posts from a text file, how can I add pagination so that it only shows 5 blog posts at a time?

Here is the script:

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

And here is the text file:

Feb 17 2010|Title|Blog post content here|[end]
Feb 17 2010|Title|Blog post content here|[end]
Feb 17 2010|Title|Blog post content here|[end]
Feb 17 2010|Title|Blog post content here|[end]

Any help is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断桥再见 2024-08-29 03:28:23

像这样的东西:(

<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> ';
}
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>";
}
?>

这完全未经测试,但希望你可以从这里获取它!)

Something like this:

<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> ';
}
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>";
}
?>

(This is completely untested, but hopefully you can take it from here!)

葬心 2024-08-29 03:28:23

这在我的测试中有效:

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}\">← 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 →</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;
    }
}

This worked in my tests:

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}\">← 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 →</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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文