简体   繁体   English

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

[英]Help With PHP Pagination Script For Flat File Database

I have a few questions regarding a PHP Pagination Script For Flat File Database I found. 我发现了一些有关平面文件数据库的PHP分页脚本的问题。 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: 我的第一个问题似乎在第9行。我可以将行更改为:

$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? 需要做什么到第9行才能消除我的页面错误?

Secondly, I would like to fetch the data on line 5 in a different way. 其次,我想以另一种方式在第5行获取数据。 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. 我想从一个文本文件中获取数据,我们将其称为“ items.txt”,其中包含如下条目,每行一个。

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

Problem With Line 9 第9行的问题

$_REQUEST[page] has two separate problems. $_REQUEST[page]有两个独立的问题。

1) page is being read as the name of a constant because it is not quoted. 1)由于未引用page被读取为常量的名称。 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. PHP然后注意到有没有所谓的固定page ,所以它需要一个猜测,你的意思是串page -这将是,如果page被引用-并触发一个错误时通知您。 Therefore, use $_REQUEST['page'] instead. 因此,请改用$_REQUEST['page']

2) 'page' is not necessarily a key of $_REQUEST because the data is not guaranteed to be given. 2) 'page'不一定是$_REQUEST的键,因为不能保证会给出数据。 Therefore, you cannot refer to $_REQUEST['page'] before you ensure that it exists. 因此,在确保$_REQUEST['page']存在之前,您不能对其进行引用。 This may be done by isset($_REQUEST['page']) . 这可以通过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. file()函数将file()的行读取到数组中-例如,数组的第四个值也是文件中的第四行。 Therefore, you can set $dataArray simply as $dataArray = file('text.dat'); 因此,你可以设置$dataArray简称为$dataArray = file('text.dat'); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM