Help With PHP Pagination Script For Flat File Database

半世苍凉 提交于 2019-12-06 14:57:38

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/5968458/help-with-php-pagination-script-for-flat-file-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!