Re: How to Page Through Database Results
| From: | Yurais Fernandez Leal | Date: | Mon, 02 Oct 2000 21:57:13 +0000 |
| Subject: | Re: How to Page Through Database Results | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18323@lists.php.net to get a copy of this message | ||
Perfect... and how to create the Previous 20 link ?
On Thu, 28 Sep 2000, Matt McClanahan wrote:
cardin: On Thu, 28 Sep 2000, Ian Plount wrote:
cardin:
cardin: > Can somebody point me to a tutorial or example of how to page through a
cardin: > result set from a database?
cardin: >
cardin: > For example say the result set from the database contains 100 records.
cardin: > I want to be able to press either a forward or back button to page
cardin: > through the records 20 at a time. I would also like buttons to be able
cardin: > to go to the first or the last page.
cardin:
cardin: Pretty straightforward. The only part to worry about is setting the limit
cardin: <offset>,<limit> portion of your SQL query.
cardin:
cardin: <?
cardin:
cardin: $limit = 20;
cardin: if (!isset($offset))
cardin: $offset = 0;
cardin:
cardin: $sql = "SELECT Lastname,Firstname FROM MyTable ORDER BY Lastname " .
cardin: "LIMIT $offset,$limit";
cardin: $res = mysql_query($sql);
cardin:
cardin: while ($row = mysql_fetch_row($res))
cardin: echo "$row[0], $row[1]<br>";
cardin:
cardin: mysql_free_result($res);
cardin:
cardin: $offset += $limit;
cardin:
cardin: echo "<br><a href=\"$PHP_SELF?offset=$offset\">Next $limit
Entries</a>";
cardin:
cardin: ?>
cardin:
cardin: HTH,
cardin: Matt
cardin:
cardin:
cardin:
--
------------------------------------------------
Yurais Fernández Leal, Systems Administrator
Public Health Network Santiago de Cuba
yurais@medired.scu.sld.cu
http://www.scu.sld.cu/
Phone: (53) 226-54204
(home) (53) 226-51556
FAX: (53) 226-54204
ICQ: 33792610
------------