平面文件数据库的 PHP 分页脚本帮助

发布于 2024-11-06 18:05:43 字数 1749 浏览 0 评论 0原文

我对我找到的平面文件数据库的 PHP 分页脚本有一些疑问。 我已经发布了下面的脚本。

<?php 
echo '<html><body>';

// Data, normally from a flat file or some other source  
$data = "Item1|Item2|Item3|Item4|Item5|Item6|Item7|Item8|Item9|Item10";  
// Put our data into an array  
$dataArray = explode('|', $data);  
// Get the current page  
$currentPage = trim($_REQUEST[page]);  
// Pagination settings  
$perPage = 3;  
$numPages = ceil(count($dataArray) / $perPage);  
if(!$currentPage || $currentPage > $numPages)  
    $currentPage = 0;  
$start = $currentPage * $perPage;  
$end = ($currentPage * $perPage) + $perPage;  
// Extract ones we need  
foreach($dataArray AS $key => $val)  
{  
    if($key >= $start && $key < $end)  
        $pagedData[] = $dataArray[$key];  
}

foreach($pagedData AS $item)  
    echo '<a href="/'. $item .'/index.php">'. $item .'</a><br>';

if($currentPage > 0 && $currentPage < $numPages)  
    echo '<a href="?page=' . ($currentPage - 1) . '">« Previous page</a><br>';  
if($numPages > $currentPage && ($currentPage + 1) < $numPages)  
    echo '<a href="?page=' . ($currentPage + 1) . '" class="right">Next page »</a><br>';

echo '</body></html>';
?>

我的第一个问题似乎在第 9 行。我可以将该行更改为:

$currentPage = trim(@$_REQUEST[page]);

但此更改不会修复错误,它只会隐藏它。需要对第 9 行做什么才能消除我的页面上的错误?

其次,我想以不同的方式获取第 5 行的数据。我想从一个文本文件中获取数据,我们称之为“items.txt”,其中包含如下条目,每行一个。

Fun
Games
Toys
Sports
Fishing
Pools
Boats

请推荐替代代码来获取所需的数据。

最后,我想包括指向“第一页”和“最后一页”以及“上一页”和“下一页”的链接,就像当前的代码一样。

我为我的草率发帖表示歉意,但非常感谢任何可以帮助我了解产生我想要的结果所需的更改的人。谢谢.....

I have a few questions regarding a PHP Pagination Script For Flat File Database I found.
I have posted the script below.

<?php 
echo '<html><body>';

// Data, normally from a flat file or some other source  
$data = "Item1|Item2|Item3|Item4|Item5|Item6|Item7|Item8|Item9|Item10";  
// Put our data into an array  
$dataArray = explode('|', $data);  
// Get the current page  
$currentPage = trim($_REQUEST[page]);  
// Pagination settings  
$perPage = 3;  
$numPages = ceil(count($dataArray) / $perPage);  
if(!$currentPage || $currentPage > $numPages)  
    $currentPage = 0;  
$start = $currentPage * $perPage;  
$end = ($currentPage * $perPage) + $perPage;  
// Extract ones we need  
foreach($dataArray AS $key => $val)  
{  
    if($key >= $start && $key < $end)  
        $pagedData[] = $dataArray[$key];  
}

foreach($pagedData AS $item)  
    echo '<a href="/'. $item .'/index.php">'. $item .'</a><br>';

if($currentPage > 0 && $currentPage < $numPages)  
    echo '<a href="?page=' . ($currentPage - 1) . '">« Previous page</a><br>';  
if($numPages > $currentPage && ($currentPage + 1) < $numPages)  
    echo '<a href="?page=' . ($currentPage + 1) . '" class="right">Next page »</a><br>';

echo '</body></html>';
?>

My first problem seems to be in line 9. I could change the line to:

$currentPage = trim(@$_REQUEST[page]);

But this change won't fix the error, it will just hide it. What needs to be done to line 9 to rid my page of the error?

Secondly, I would like to fetch the data on line 5 in a different way. I would like to get the data from a text file, let's call it "items.txt", that has entries like below, one per line.

Fun
Games
Toys
Sports
Fishing
Pools
Boats

Please recommend alternate code to fetch the desired data.

Lastly, I would like to include links to the "First page" and "Last page" as well as "Previous page" and "Next page", as is the current code.

I apologize for my sloppy posting, but would be real appreciative of anybody who could help me understand the changes needed to produce my desired results. Thanks.....

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

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

发布评论

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

评论(1

手心的温暖 2024-11-13 18:05:43

第 9 行出现问题

$_REQUEST[page] 有两个单独的问题。

1) page 被读取为常量名称,因为它没有被引用。然后,PHP 注意到没有名为 page 的常量,因此它会猜测您的意思是字符串 page ——如果 page 则为字符串被引用——并触发错误来通知您。因此,请改用 $_REQUEST['page']

2) 'page' 不一定是 $_REQUEST 的键,因为不保证给出数据。因此,在确保 $_REQUEST['page'] 存在之前,您不能引用它。这可以通过 isset($_REQUEST['page']) 来完成。

那么你的最终代码应该看起来像这样。

if (isset($_REQUEST['page'])) {
    $currentPage = $_REQUEST['page'];
} else {
    $currentPage = 'some default value';
}

数据源问题

file() 函数将文件的行读取到数组中 - 例如,数组的第四个值也是文件中的第四行文件。因此,您可以将 $dataArray 简单地设置为 $dataArray = file('text.dat');

Problem With Line 9

$_REQUEST[page] has two separate problems.

1) page is being read as the name of a constant because it is not quoted. PHP then notices there is no constant called page, so it takes a guess that you meant the string page -- which it would be if page was quoted -- and triggers an error to notify you. Therefore, use $_REQUEST['page'] instead.

2) 'page' is not necessarily a key of $_REQUEST because the data is not guaranteed to be given. Therefore, you cannot refer to $_REQUEST['page'] before you ensure that it exists. This may be done by isset($_REQUEST['page']).

Your final code should look something like this, then.

if (isset($_REQUEST['page'])) {
    $currentPage = $_REQUEST['page'];
} else {
    $currentPage = 'some default value';
}

Problem With Data Source

The file() function reads the lines of a file into an array -- for example, the fourth value of the array is also the fourth line in the file. Therefore, you can set $dataArray simply as $dataArray = file('text.dat');.

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