0

我一直在使用 php 中的数组进行分页。我有一系列帖子,我用它们将页面的内容分成更小的块。它可以按我的意愿工作并返回内容。

<?php
// let's paginate data from an array...
$posts = array(
// array of posts

"blog/posts/06-19-tues.php",
"blog/posts/06-16-sat.php",

"blog/posts/05-26-sat.php",
"blog/posts/05-23-wed.php",
"blog/posts/05-09-wed.php"

);

// how many records should be displayed on a page?
$records_per_page = 3;

// include the pagination class
require 'zebra/Zebra_Pagination.php';

// instantiate the pagination object
$pagination = new Zebra_Pagination();

// the number of total records is the number of records in the array
$pagination->records(count($posts));

// records per page
$pagination->records_per_page($records_per_page);

// here's the magick: we need to display *only* the records for the current page
$posts = array_slice(
    $posts,
    (($pagination->get_page() - 1) * $records_per_page),
    $records_per_page
);

?>

<?php foreach ($posts as $index => $post):?>
<?php include $post; ?>
<?php endforeach?>

<?php

// render the pagination links
$pagination->render();

?>

我现在的问题是如何链接到网站其他地方的各个帖子。由于它们最终会从一个页面移动到另一个页面,因此直接链接到静态文件将不起作用。起初,我给每个帖子一个唯一的 id 并用它来链接到帖子,但现在这不起作用,因为链接会动态变化。

我看过 array_search() ,它看起来很有希望,但我不明白它足以让它产生一个超链接。

我不确定我是否已经很好地表达了这个问题。如果我没有多大意义,请道歉。

4

1 回答 1

0

如果我理解正确,我认为这样的事情会起作用:

if (isset($_REQUEST['page']) {
  $found = array_search($_REQUEST['page'], $posts);
  if ($found) {
    $pagination->set_page(floor($found/$records_per_page)+1);
  }
}

然后你可以使用像这样的链接

$link = '<a href="yourpage.php?page=' . urlencode($posts[$i]) . '">whatever</a>';
于 2013-02-11T19:33:37.187 回答