Re: How to Page Through Database Results
| From: | Matt McClanahan | Date: | Thu, 28 Sep 2000 23:36:37 +0000 |
| Subject: | Re: How to Page Through Database Results | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-17980@lists.php.net to get a copy of this message | ||
On Thu, 28 Sep 2000, Ian Plount wrote:
> Can somebody point me to a tutorial or example of how to page through a
> result set from a database?
>
> For example say the result set from the database contains 100 records.
> I want to be able to press either a forward or back button to page
> through the records 20 at a time. I would also like buttons to be able
> to go to the first or the last page.
Pretty straightforward. The only part to worry about is setting the limit
<offset>,<limit> portion of your SQL query.
<?
$limit = 20;
if (!isset($offset))
$offset = 0;
$sql = "SELECT Lastname,Firstname FROM MyTable ORDER BY Lastname " .
"LIMIT $offset,$limit";
$res = mysql_query($sql);
while ($row = mysql_fetch_row($res))
echo "$row[0], $row[1]<br>";
mysql_free_result($res);
$offset += $limit;
echo "<br><a href=\"$PHP_SELF?offset=$offset\">Next $limit
Entries</a>";
?>
HTH,
Matt