NYCPHP Meetup

NYPHP.org

[nycphp-talk] understanding PEAR::DB_Pager

David Mintz dmintz at davidmintz.org
Fri Sep 3 12:44:00 EDT 2004


If any of you are using the PEAR DB_Pager (0.7) class with success, maybe
you can help me figure this out. Here's a snippet based closely on the
example in the source:

<?php
require_once 'DB/Pager.php';
$sql= "select * from events"; // has 20K+ rows
$db = DB::connect($DSN);
$from = 100;   // The row to start to fetch from, which you typically get
		// from $_GET. we are simulating that the user
		// clicked a link pointing to a page 5 (of 852)
$limit = 25; 	// The number of results per page
$maxpages = 15; // The number of pages for displaying in the pager

$res   = $db->limitQuery($sql, $from, $limit);
$nrows = $db->getOne("select count(*) from events");

echo  "rows: $nrows\n";

$nrows = $res->numRows();
while ($row = $res->fetchrow()) {
   // display data
}

$data = DB_Pager::getData($from, $limit, $nrows, $maxpages);
print_r( $data );
?>

The author encourages us to use the array returned by getData() to build
our linked page numbers. The build() method, constructor etc are now
deprecated in favor of this one static getData() method.

Above, getData() returns an array that looks like this:

Array
(
    [numpages] => 852
    [firstpage] => 1
    [lastpage] => 852
    [pages] => Array
        (
            [1] => 0
            [2] => 25
            [3] => 50
            [4] => 75
            [5] => 100
            [6] => 125
            [7] => 150
            [8] => 175
            [9] => 200
            [10] => 225
            [11] => 250
        )

    [current] => 5
    [maxpages] => 15
    [prev] => 75
    [next] => 125
    [remain] => 25
    [to] => 125
    [numrows] => 21297
    [from] => 101
    [limit] => 25
)


It's the $data['pages'] array that I don't get. When the offset is 0, it
contains just 7 elements, not the 15 that I want:

[pages] => Array
        (
            [1] => 0
            [2] => 25
            [3] => 50
            [4] => 75
            [5] => 100
            [6] => 125
            [7] => 150
        )

When offset is 75, it has 10 elements:

   [pages] => Array
        (
            [1] => 0
            [2] => 25
            [3] => 50
            [4] => 75
            [5] => 100
            [6] => 125
            [7] => 150
            [8] => 175
            [9] => 200
            [10] => 225
        )
}

When offset = 100, it has 11. When offset = 175 or more, it has 14.  When
offset exceeds 175, $data['pages'] starts "sliding" up as expected, so
when offset is 675:

    [pages] => Array
        (
            [21] => 500
            [22] => 525
            [23] => 550
            [24] => 575
            [25] => 600
            [26] => 625
            [27] => 650
            [28] => 675
            [29] => 700
            [30] => 725
            [31] => 750
            [32] => 775
            [33] => 800
            [34] => 825
        )

    [current] => 28
    [maxpages] => 15
    [prev] => 650
    [next] => 700
    [remain] => 25
    [to] => 700
    [numrows] => 21298
    [from] => 676
    [limit] => 25
)

But it never has 15 elements. I'm wondering whether this behavior is by
design, or I'm doing something wrong, or what. Meanwhile I have been
ignoring $data['pages'] and just doing own arithmetic to get what I want,
but that seems like a duplication of effort.

Many thanks,

---
David Mintz
http://davidmintz.org/

        "Anybody else got a problem with Webistics?" -- Sopranos 24:17



More information about the talk mailing list