count() total records for pagination with limit
| From: | PJ | Date: | Tue, 14 Apr 2009 18:49:22 +0000 |
| Subject: | count() total records for pagination with limit | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-291495@lists.php.net to get a copy of this message | ||
I seem to recall that it is possible to count all instances of a query
that is limited by $RecordsPerPage without repeating the same query. I
believe that COUNT() had to called immediately after the SELECT word but
I neglected to bookmark the source. Dummy!
I don't like the idea of count(*) over count() or something like that as
it seems rather slow fram what I read.
right now I do this:
$sql = "SELECT * FROM book
WHERE id IN (SELECT bookID
FROM book_author WHERE authID IN (SELECT author.id
FROM author WHERE LEFT(last_name, 1 ) = '$Auth')) ";
$Count1 = mysql_num_rows(mysql_query($sql, $db));
$books = array();
$SQL = "SELECT * FROM book
WHERE id IN (SELECT bookID
FROM book_author WHERE authID IN (SELECT author.id
FROM author WHERE LEFT(last_name, 1 ) = '$Auth'))
ORDER BY $sort $dir
LIMIT $offset, $records_per_page ";
if ( ( $results = mysql_query($SQL, $db) ) !== false ) {
while ( $row = mysql_fetch_assoc($results) ) {
$books[$row['id']] = $row;
}
}
$Count = mysql_num_rows($results);
$Count gives me the actual rows for display - $Count1 gives me the total
rows available.
Can this be streamlined any?
--
unheralded genius: "A clean desk is the sign of a dull mind. "
-------------------------------------------------------------
Phil Jourdan --- pj@ptahhotep.com
http://www.ptahhotep.com
http://www.chiccantine.com/andypantry.php