PHP MySql and limiting results

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
bopritchard
Forum Newbie
Posts: 1
Joined: Tue Jun 11, 2002 11:22 am

PHP MySql and limiting results

Post by bopritchard »

I've got a page that has sql that returns x number of rows...i only want to display 10 per page....and then have a Page 1, 2, 3 etc link....i know i can limit the records returned by doing limit 10 or limit 5, 10 at the end of my sql....but what about knowing how many i've got total so that i know how many page links i need to display...

for instance if i've got 47 total records i want to display Page 1,2,3,4,5 as links to forward through the results....i know i can do a select count(*) from xxxx.....but i'm sure there is another way another than performing and additional query....

and what about the php code to handle those page numbers...anyone got an example i can look at...

thanks
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Database pagination

Post by BDKR »

This is a common asked question. There is a tutorail on the topic at PHPBUILDER. There should still be one or two over at phpdeveloper.org as well.

I really say you should give it a stab yourself as it will make you a better coder. The big things you need is the limit clause in MySQL (or SQL in general), count() or mysqll_num_rows() (assumint you are using mysql) depending on whether or not you want to get the row count via MySQL or PHP, and then some simple math to figure out what rows are going to be shown on what pages.

To use your example, if there are 47 records and you want to show 5 records at a time, then we allready know how many pages we are going to need right? Ten! You get my drift.

Later on,
BDKR (TR Cox)
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post by chris12295 »

haha, i have just finished overcoming that dilema myself. email me if you want a full description of what i did. chris12295@aol.com. :P
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

GREAT!

Post by BDKR »

Right on! I will email you as a matter of fact.

I'm glald to hear,
Later on,
BDKR (TR Cox)
Post Reply