DB_Pager-0.7 + Postgres 7.2.1 question on usage, possible bug (if I am using it correctly)
| From: | Rob | Date: | Tue, 31 Dec 2002 19:45:35 +0000 |
| Subject: | DB_Pager-0.7 + Postgres 7.2.1 question on usage, possible bug (if I am using it correctly) | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-11974@lists.php.net to get a copy of this message | ||
Is this behavior intended? Below is a portion of my day and a confused
conclusing about using DB_Pager.
Pardon the long explanation, I did my best.
Here's is the usage example from DB_Pager:
*< ?php
* require_once 'DB/Pager.php';
* $db = DB::connect('your DSN string');
* $from = 0; // The row to start to fetch from (you might want to get this
* // param from the $_GET array
* $limit = 10; // The number of results per page
* $maxpages = 10; // The number of pages for displaying in the pager
(optional)
* $res = $db->limitQuery($sql, $from, $limit);
* $nrows = 0; // Alternative you could use $res->numRows()
* while ($row = $res->fetchrow()) {
* // XXX code for building the page here
* $nrows++;
* }
* $data = DB_Pager::getData($from, $limit, $nrows, $maxpages);
* // XXX code for building the pager here
* ? >
The example for DB_Pager says to setup your query like this:
$res = $db->limitQuery($sql, $from, $limit);
So far I'm OK with this, I've used it before. It puts the $limit on the
number of rows returned and uses the $from to tell the Postgres server how
much to offset (or where to start returning rows to me)
The example then goes on to have me perform a static call on DB_Pager like
this:
$data = DB_Pager::getData($from, $limit, $nrows, $maxpages);
The only problem that I run into is with $nrows. If I have a table that has
100 rows in it and I use limitQuery() to take a 25 rows of that, the
DB_Result object only has 25 rows, there isn't anywhere in the object that
says the query without the LIMIT modifier would have 100 rows. So I use
$res->numRows() and it tells me that I have 25 rows in the result set.
But I can't use 25 when I call getData from DB_Pager, because it will only
think that I have 25 rows in my table. I've got to find a way to tell it
that I actually have 100 rows even though my result set only has 25.
So, I can make a new query using COUNT(*) and the same WHERE clause of the
other query and find out that the full set has 100 rows. But this is two
queries that I have to run. (Granted the COUNT(*) function takes up
considerably less resources than actually pulling all the records and
counting them with PHP).
The example from DB_Pager lead me to believe that I could use the numRows()
method and then pass this to DB_Pager and let it do it's thing. But I can't
figure out how this works. If you use LIMIT on a query it's going to do
exactly like it sounds and limit the result set.
If I have to run a query that counts the number of records then I'm fine
with that, but if I've missed some important piece of the DB_Pager puzzle
here and someone notices my Forrest Gump'ness I'd appreciate some help. It
seems cumbersome this way and it would be cool if this could be incorporated
into DB_Pager somehow.