没有 SQL 的 PHP 动态分页

2022-01-04 00:00:00 php pagination

我有一个脚本可以动态调用和显示目录中的图像,对它进行分页的最佳方法是什么?我希望能够通过脚本中的变量控制每页显示的图像数量.我正在考虑使用 URL 变量(即 - http://domain.com/page.php?page=1) 但我不确定如何去做.

I've got a script that dynamically calls and displays images from a directory, what would be the best way to paginate this? I'd like to be able to control the number of images that are displayed per page through a variable within the script. I'm thinking of using URL varriables (ie - http://domain.com/page.php?page=1) but am unsure how to go about this.

感谢您的帮助.

推荐答案

分页是同一个概念,不管有没有 sql.你只需要你的基本变量,然后你就可以创建你想要的内容.这是一些准代码:

pagination is the same concept with or without sql. you just need your basic variables, then you can create the content you want. here's some quasi-code:

$itemsPerPage = 5;

$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
$totalItems = getTotalItems();
$totalPages = ceil($totalItems / $itemsPerPage);

function getTotalItems() {
// since they're images, perhaps we'll scan a directory of images to determine
// how many images we have in total
}

function getItemsFromPage($page, $itemsPerPage) {
// function to grab $itemsPerPage based on which $page we're on
}

function getPager($totalPages, $currentPage) {
// build your pager
}

希望能帮助您入门!

相关文章