开发者

Should I use a physical page for a list?

I have a table with quite a few records. Now at the moment I select them on one page. But I want to create a list. The list would contain multiple (about 5-10) pages.

For this, should 开发者_如何学JAVAI use physical (page1.php | page2.php | page3.php) or virtual pages (page.php?number=1 | page.php?number=2 | page.php?number3)?


I'm going to rephrase your question in my answer and then answer my question...

Quick Overview

What you have is a result set, and this result set you would like to be able to paginate the results so that you do not display all of the items in your results all at once. You would like to be able to display any amount of results per page, but most likely something like 5 or 10 results.

Answer

Okay, lets assume that you have your result set which is loaded when you view index.php.

What you want to do however is only display a set amount of items from within that result set.

I will make up an imaginary result set now.

$results = array( 
array( 'id' => 1, 'Apple'),
array( 'id' => 2, 'Orange'),
array( 'id' => 3, 'Banana'),
array( 'id' => 4, 'Pinapple'),
array( 'id' => 5, 'Kiwi'),
array( 'id' => 6, 'Starberries'),
array( 'id' => 7, 'Blackberries'),
array( 'id' => 8, 'Melon'),
array( 'id' => 9, 'Opal'),
array( 'id' => 10, 'Zuchini'),
array( 'id' => 11, 'Peach'),
array( 'id' => 12, 'Pear'),
array( 'id' => 13, 'Plum')
);

Okay, so now, this can be our imaginary result set. What you would like to do, for page 1 is retrieve only the first 5 items.

So when someone visits index.php?page=1 or index.php, they will only get (Apple, Orange, Banana, Pinapple and Kiwi).

This is called pagination. You can either paginate through results after you have retrieved them, or you can paginate before you have the results by using offsets and limits in mySQL. Because you haven't said you are using mySQL I have made the assumption that you have all your results already.

Now, if you have a request that comes in like

GET /index.php?page=1

Then you would do this:

$page = $_GET['page'];
$page ? is_numeric($page) && isset($page) ? $page : 1;
$count = 5;
$offset = $page == 1 ? 0 : (($page * $count) -1 ) - $count;
// Slice now contains your results which you can use to foreach() through    
$slice = array_slice($results, $offset,$count);

Result

Array
(
    [0] => Array
        (
            [id] => 1
            [0] => Apple
        )

    [1] => Array
        (
            [id] => 2
            [0] => Orange
        )

    [2] => Array
        (
            [id] => 3
            [0] => Banana
        )

    [3] => Array
        (
            [id] => 4
            [0] => Pinapple
        )

    [4] => Array
        (
            [id] => 5
            [0] => Kiwi
        )

)

In this example, I haven't done any actual filtering or any serious validation that the $_GET page is the correct format, but you should get the idea.

There are also plenty of resources to make this easier for you. I personally am a massive fan of Zend_Paginator which I use for all my pagination needs. See here Zend_Paginator

Also other resources:

  • http://www.lotsofcode.com/php/php-array-pagination.htm

  • http://tiffanybbrown.com/2008/12/14/simple-pagination-for-arrays-with-php-5/


Definitly use one page where you pass a page number to.
Otherwise it is nearly static, because you would need to create an additional page every time you need "more pages", but if you pass e.g. an offset to one page you can just add that to the query using 'LIMIT offset,pagesize'.

Something like this:

$pagesize = 30;
$data = YOUR_QUERY_METHOD(YOUR_QUERY . ' LIMIT ' . $pagesize*$_GET['number'] . ', ' . $pagesize);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜