帮助平面文件数据库的PHP分页脚本 [英] Help With PHP Pagination Script For Flat File Database

查看:142
本文介绍了帮助平面文件数据库的PHP分页脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个问题关于PHP分页脚本对于平面文件数据库我发现。
我已经发布了下面的脚本。

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

//通常来自平面文件或其他来源的数据
$ data =Item1 | Item2 | Item3 | Item4 | Item5 | Item6 | Item7 | Item8 | Item9 | Item10;
//将数据放入数组
$ dataArray = explode('|',$ data);
//获取当前页面
$ currentPage = trim($ _ REQUEST [page]);
//分页设置
$ perPage = 3;
$ numPages = ceil(count($ dataArray)/ $ perPage);
if(!$ currentPage || $ currentPage> $ numPages)
$ currentPage = 0;
$ start = $ currentPage * $ perPage;
$ end =($ currentPage * $ perPage)+ $ perPage;
//提取需要的数据
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)。 '>«上一页< / a>< br>';
if($ numPages> $ currentPage&&($ currentPage + 1)< $ numPages)
echo'< a href =?page = 。''class =right>下一页»< / a>< br>';

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

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

  $ currentPage = trim(@ $ _ REQUEST [page]); 

但这个更改不会修复错误,它会隐藏它。第9行需要做什么来摆脱我的页面错误?



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

  Fun 
游戏
玩具
运动
钓鱼
泳池
船只

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



最后,我想添加指向第一页和最后一页以及上一页和下一页,如当前代码。



对于我的草稿,我深表歉意,但对于任何可以帮助我理解产生我想要的结果所需的变化的人,我将深表谢意。感谢.....

解决方案

第9行问题



$ _ REQUEST [page] 有两个独立的问题。



1) page 正被读取为常量的名称,因为它没有引号。 PHP然后注意没有常量调用,所以它需要一个猜测,你的意思是字符串 - 它会是如果 page 被引用 - 并触发一个错误通知你。因此,请改用 $ _ REQUEST ['page']



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



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

数据源问题 b
$ b

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

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

解决方案

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

这篇关于帮助平面文件数据库的PHP分页脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆