【问题标题】:How to create a hyperlink to a file in an array (paginated files)如何创建指向数组中文件的超链接(分页文件)
【发布时间】:2013-02-12 03:33:37
【问题描述】:

我一直在使用 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(),它看起来很有希望,但我不明白它是否足以让它生成超链接。

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

【问题讨论】:

  • 您的意思是指向分页中特定页面的链接吗?
  • 不,不是特定页面而是特定文件,例如图像。在第一页上,我可以通过直接指向文件来链接到它。这会工作一段时间,直到它移动到第 2 页,因为有更多内容。
  • 如果你没有链接到分页,为什么直接链接到目标文件时页码很重要?
  • 我希望能够链接到主页上下文中的文件并直接链接到文件本身。

标签: php html dynamic hyperlink pagination


【解决方案1】:

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

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

【讨论】:

  • 谢谢。我还没有机会尝试它,但它看起来确实是我需要的。
猜你喜欢
  • 2022-01-27
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多